Module Shoulda::ActionController::Macros
In: lib/shoulda/action_controller/macros.rb
ValidationMatcher ValidatePresenceOfMatcher ValidateAcceptanceOfMatcher EnsureLengthOfMatcher ValidateFormatOfMatcher EnsureInclusionOfMatcher ValidateUniquenessOfMatcher ValidateNumericalityOfMatcher Context SetSessionMatcher RespondWithContentTypeMatcher SetTheFlashMatcher RenderWithLayout RespondWithMatcher AssignToMatcher FilterParamMatcher RouteMatcher HaveNamedScopeMatcher HaveDbIndexMatcher HaveDbColumnMatcher AllowMassAssignmentOfMatcher AllowValueMatcher AssociationMatcher HaveReadonlyAttributeMatcher lib/shoulda/context.rb lib/shoulda/action_controller/matchers/set_session_matcher.rb lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb lib/shoulda/action_controller/matchers/respond_with_matcher.rb lib/shoulda/action_controller/matchers/assign_to_matcher.rb lib/shoulda/action_controller/matchers/filter_param_matcher.rb lib/shoulda/action_controller/matchers/route_matcher.rb Matchers Macros ActionController lib/shoulda/active_record/matchers/validation_matcher.rb lib/shoulda/active_record/matchers/have_named_scope_matcher.rb lib/shoulda/active_record/matchers/validate_numericality_of_matcher.rb lib/shoulda/active_record/matchers/have_db_index_matcher.rb lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb lib/shoulda/active_record/matchers/validate_format_of_matcher.rb lib/shoulda/active_record/matchers/validate_acceptance_of_matcher.rb lib/shoulda/active_record/matchers/have_db_column_matcher.rb lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb lib/shoulda/active_record/matchers/allow_value_matcher.rb lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb lib/shoulda/active_record/matchers/association_matcher.rb lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb Matchers Helpers Assertions Macros ActiveRecord ClassMethods Macros ActionView Private Helpers Assertions ActionMailer InstanceMethods Assertions Macros Shoulda dot/m_47_0.png

Macro test helpers for your controllers

By using the macro helpers you can quickly and easily create concise and easy to read test suites.

This code segment:

  context "on GET to :show for first record" do
    setup do
      get :show, :id => 1
    end

    should_assign_to :user
    should_respond_with :success
    should_render_template :show
    should_not_set_the_flash

    should "do something else really cool" do
      assert_equal 1, assigns(:user).id
    end
  end

Would produce 5 tests for the show action

Methods

Included Modules

Matchers

Public Instance methods

Macro that creates a test asserting that the controller assigned to each of the named instance variable(s).

Options:

  • :class - The expected class of the instance variable being checked.

If a block is passed, the assigned variable is expected to be equal to the return value of that block.

Example:

  should_assign_to :user, :posts
  should_assign_to :user, :class => User
  should_assign_to(:user) { @user }

[Source]

     # File lib/shoulda/action_controller/macros.rb, line 87
 87:       def should_assign_to(*names, &block)
 88:         klass = get_options!(names, :class)
 89:         names.each do |name|
 90:           matcher = assign_to(name).with_kind_of(klass)
 91:           should matcher.description do
 92:             if block
 93:               expected_value = instance_eval(&block)
 94:               matcher = matcher.with(expected_value)
 95:             end
 96: 
 97:             assert_accepts matcher, @controller
 98:           end
 99:         end
100:       end

Macro that creates a test asserting that filter_parameter_logging is set for the specified keys

Example:

  should_filter_params :password, :ssn

[Source]

    # File lib/shoulda/action_controller/macros.rb, line 64
64:       def should_filter_params(*keys)
65:         keys.each do |key|
66:           matcher = filter_param(key)
67:           should matcher.description do
68:             assert_accepts matcher, @controller
69:           end
70:         end
71:       end

Macro that creates a test asserting that the controller did not assign to any of the named instance variable(s).

Example:

  should_not_assign_to :user, :posts

[Source]

     # File lib/shoulda/action_controller/macros.rb, line 108
108:       def should_not_assign_to(*names)
109:         names.each do |name|
110:           matcher = assign_to(name)
111:           should "not #{matcher.description}" do
112:             assert_rejects matcher, @controller
113:           end
114:         end
115:       end

Macro that creates a test asserting that the flash is empty.

[Source]

    # File lib/shoulda/action_controller/macros.rb, line 51
51:       def should_not_set_the_flash
52:         matcher = set_the_flash
53:         should "not #{matcher.description}" do
54:           assert_rejects matcher, @controller
55:         end
56:       end

Macro that creates a test asserting that the controller returned a redirect to the given path. The passed description will be used when generating a test name. Expects a block that returns the expected path for the redirect.

Example:

  should_redirect_to("the user's profile") { user_url(@user) }

[Source]

     # File lib/shoulda/action_controller/macros.rb, line 199
199:       def should_redirect_to(description, &block)
200:         should "redirect to #{description}" do
201:           expected_url = instance_eval(&block)
202:           assert_redirected_to expected_url
203:         end
204:       end

Macro that creates a test asserting that the controller rendered the given template. Example:

  should_render_template :new

[Source]

     # File lib/shoulda/action_controller/macros.rb, line 162
162:       def should_render_template(template)
163:         should "render template #{template.inspect}" do
164:           assert_template template.to_s
165:         end
166:       end

Macro that creates a test asserting that the controller rendered with the given layout. Example:

  should_render_with_layout 'special'

[Source]

     # File lib/shoulda/action_controller/macros.rb, line 172
172:       def should_render_with_layout(expected_layout = 'application')
173:         matcher = render_with_layout(expected_layout)
174:         if expected_layout
175:           should matcher.description do
176:             assert_accepts matcher, @controller
177:           end
178:         else
179:           should "render without layout" do
180:             assert_rejects matcher, @controller
181:           end
182:         end
183:       end

Macro that creates a test asserting that the controller rendered without a layout. Same as @should_render_with_layout false@

[Source]

     # File lib/shoulda/action_controller/macros.rb, line 187
187:       def should_render_without_layout
188:         should_render_with_layout nil
189:       end

Macro that creates a test asserting that the controller responded with a ‘response’ status code. Example:

  should_respond_with :success

[Source]

     # File lib/shoulda/action_controller/macros.rb, line 121
121:       def should_respond_with(response)
122:         should "respond with #{response}" do
123:           matcher = respond_with(response)
124:           assert_accepts matcher, @controller
125:         end
126:       end

Macro that creates a test asserting that the response content type was ‘content_type’. Example:

  should_respond_with_content_type 'application/rss+xml'
  should_respond_with_content_type :rss
  should_respond_with_content_type /rss/

[Source]

     # File lib/shoulda/action_controller/macros.rb, line 134
134:       def should_respond_with_content_type(content_type)
135:         matcher = respond_with_content_type(content_type)
136:         should matcher.description do
137:           assert_accepts matcher, @controller
138:         end
139:       end

Macro that creates a routing test. It tries to use the given HTTP method on the given path, and asserts that it routes to the given options.

If you don‘t specify a :controller, it will try to guess the controller based on the current test.

to_param is called on the options given.

Examples:

  should_route :get, "/posts", :controller => :posts, :action => :index
  should_route :get, "/posts/new", :action => :new
  should_route :post, "/posts", :action => :create
  should_route :get, "/posts/1", :action => :show, :id => 1
  should_route :edit, "/posts/1", :action => :show, :id => 1
  should_route :put, "/posts/1", :action => :update, :id => 1
  should_route :delete, "/posts/1", :action => :destroy, :id => 1
  should_route :get, "/users/1/posts/1",
    :action => :show, :id => 1, :user_id => 1

[Source]

     # File lib/shoulda/action_controller/macros.rb, line 227
227:       def should_route(method, path, options)
228:         unless options[:controller]
229:           options[:controller] = self.name.gsub(/ControllerTest$/, '').tableize
230:         end
231: 
232:         matcher = route(method, path).to(options)
233: 
234:         should matcher.description do
235:           assert_accepts matcher.in_context(self), self
236:         end
237:       end

Macro that creates a test asserting that a value returned from the session is correct. Expects the session key as a parameter, and a block that returns the expected value.

Example:

  should_set_session(:user_id) { @user.id }
  should_set_session(:message) { "Free stuff" }

[Source]

     # File lib/shoulda/action_controller/macros.rb, line 149
149:       def should_set_session(key, &block)
150:         matcher = set_session(key)
151:         should matcher.description do
152:           expected_value = instance_eval(&block)
153:           matcher = matcher.to(expected_value)
154:           assert_accepts matcher, @controller
155:         end
156:       end

Macro that creates a test asserting that the flash contains the given value. Expects a String or Regexp.

If the argument is nil, it will assert that the flash is not set. This behavior is deprecated.

Example:

  should_set_the_flash_to "Thank you for placing this order."
  should_set_the_flash_to /created/i

[Source]

    # File lib/shoulda/action_controller/macros.rb, line 37
37:       def should_set_the_flash_to(val)
38:         if val
39:           matcher = set_the_flash.to(val)
40:           should matcher.description do
41:             assert_accepts matcher, @controller
42:           end
43:         else
44:           warn "[DEPRECATION] should_set_the_flash_to nil is deprecated. " <<
45:                "Use should_not_set_the_flash instead."
46:           should_not_set_the_flash
47:         end
48:       end

[Validate]