Module: Doa::ClassMethods

Defined in:
lib/doa/class_methods.rb

Overview

Methods available at the context level.

Instance Method Summary collapse

Instance Method Details

#action(name, method = nil, &block) ⇒ Object

Setup a context to test a controller’s action using #do_action. It takes the action’s name and an optional HTTP method, evaluating the supplied block in the created context.



42
43
44
45
46
47
48
49
# File 'lib/doa/class_methods.rb', line 42

def action(name, method = nil, &block)
  d = describe "\##{name}" do
    do_action name, method
  end

  d.instance_eval(&block)
  d
end

#do_action(action_name, method = nil, params = Hash.new, &block) ⇒ Object

Infrastructure method used by #action to generate the methods available within a test case.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/doa/class_methods.rb', line 53

def do_action(action_name, method = nil, params = Hash.new, &block)
  # Locking in the arguments with closures
  define_method("action_method") do
    method || infer_method(action_name)
  end

  define_method("action_name") do
    action_name
  end

  define_method("shared_params") do
    return instance_eval(&block) if block
    return Hash.new
  end

  define_method("action_params") do
    params
  end

  # Now the methods we really want to use
  class_eval do
    include Doa::InstanceMethods
  end
end

#params(&block) ⇒ Object

Initialize the parameters to be used within a context inheriting those of the surrounding contexts. Takes a block which must return a Hash.

Usage:

params do
  { :id => @user.id }
end


35
36
37
# File 'lib/doa/class_methods.rb', line 35

def params(&block)
  self.default_params = block
end