Module: Penknife::Spec::ControllerExampleGroupExtensions

Includes:
ActionController::StatusCodes
Defined in:
lib/penknife/spec/controller_example_group_extensions.rb

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



6
7
8
9
10
# File 'lib/penknife/spec/controller_example_group_extensions.rb', line 6

def self.extended(base)
  base.class_eval do
    include InstanceMethods
  end
end

Instance Method Details

#default_action_method_for(action) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/penknife/spec/controller_example_group_extensions.rb', line 74

def default_action_method_for(action)
  case action.to_sym
    when :create then :post
    when :update then :put
    when :destroy then :delete
    else :get
  end
end

#describe(*args, &example_group_block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/penknife/spec/controller_example_group_extensions.rb', line 12

def describe(*args, &example_group_block)
  if example_group_block
    super
  else
    extract_action_from_args!(args)
    extract_parameters_from_args(*args)
    returning(clazz = super(*args, &example_group_block)) do
      clazz.class_eval %{
        before(:each) do
          @it = @controller
          params.merge!(self.class.described_action_parameters.dup)
          request.env["HTTP_REFERER"] = "http://test.host/originalurl"
          request.env['HTTP_USER_AGENT'] = "test"
        end

        def do_request
          send self.class.described_action_method, self.class.described_action, params
        end
      }
    end
  end
end

#described_actionObject



35
36
37
# File 'lib/penknife/spec/controller_example_group_extensions.rb', line 35

def described_action
  @described_action || (superclass.respond_to?(:described_action) ? superclass.described_action : nil)
end

#described_action_methodObject



39
40
41
# File 'lib/penknife/spec/controller_example_group_extensions.rb', line 39

def described_action_method
  @described_action_method || (superclass.respond_to?(:described_action_method) ? superclass.described_action_method : nil)
end

#described_action_parametersObject



43
44
45
# File 'lib/penknife/spec/controller_example_group_extensions.rb', line 43

def described_action_parameters
  (superclass.respond_to?(:described_action_parameters) ? superclass.described_action_parameters : {}).merge(@described_action_parameters || {})
end

#description_partsObject



68
69
70
71
72
# File 'lib/penknife/spec/controller_example_group_extensions.rb', line 68

def description_parts 
  returning(super) do |parts|
    parts << " with #{described_action_parameters.inspect}" unless described_action_parameters.empty?
  end
end

#extract_action_from_args!(args) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/penknife/spec/controller_example_group_extensions.rb', line 47

def extract_action_from_args!(args)
  options = args.extract_options!
  args.collect! do |argument| 
    if @described_action.nil? && argument.is_a?(Symbol)
      @described_action = args.first
      @described_action_method = options[:method] || default_action_method_for(@described_action)
      "doing #{@described_action_method.to_s.upcase} #{described_action}"
    else
      argument
    end
  end
  args << options
end

#extract_parameters_from_args(*args) ⇒ Object



61
62
63
64
65
66
# File 'lib/penknife/spec/controller_example_group_extensions.rb', line 61

def extract_parameters_from_args(*args)
  options = args.dup.extract_options!
  if (options[:with_params] || options[:params]) 
    @described_action_parameters = (options[:with_params] || options[:params]) 
  end
end

#it_should_redirect_to(*args) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/penknife/spec/controller_example_group_extensions.rb', line 91

def it_should_redirect_to(*args)
  path_hash = args.extract_options!
  message ||= path_hash.inspect
  it "should redirect to #{message}" do
    do_request
    @response.should redirect_to(path_hash)
  end
end

#it_should_respond_with(status) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/penknife/spec/controller_example_group_extensions.rb', line 83

def it_should_respond_with(status)
  raise "Status #{status} not known" unless status = SYMBOL_TO_STATUS_CODE[status]
  it "should respond with status #{status} (#{STATUS_CODES[status]})" do
    do_request
    response.code.to_s.should == status.to_s
  end
end