Module ThoughtBot::Shoulda::Controller::ClassMethods
In: lib/shoulda/controller_tests/controller_tests.rb

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

Furthermore, the should_be_restful helper will create an entire set of tests which will verify that your controller responds restfully to a variety of requested formats.

Constants

VALID_FORMATS = Dir.glob(File.join(File.dirname(__FILE__), 'formats', '*.rb')).map { |f| File.basename(f, '.rb') }.map(&:to_sym)   Formats tested by should_be_restful. Defaults to [:html, :xml]
VALID_ACTIONS = [:index, :show, :new, :edit, :create, :update, :destroy]   Actions tested by should_be_restful

should_be_restful

Generates a full suite of tests for a restful controller.

The following definition will generate tests for the index, show, new, edit, create, update and destroy actions, in both html and xml formats:

  should_be_restful do |resource|
    resource.parent = :user

    resource.create.params = { :title => "first post", :body => 'blah blah blah'}
    resource.update.params = { :title => "changed" }
  end

This generates about 40 tests, all of the format:

  "on GET to :show should assign @user."
  "on GET to :show should not set the flash."
  "on GET to :show should render 'show' template."
  "on GET to :show should respond with success."
  "on GET to :show as xml should assign @user."
  "on GET to :show as xml should have ContentType set to 'application/xml'."
  "on GET to :show as xml should respond with success."
  "on GET to :show as xml should return <user/> as the root element."

The resource parameter passed into the block is a ResourceOptions object, and is used to configure the tests for the details of your resources.

Public Instance methods

[Source]

     # File lib/shoulda/controller_tests/controller_tests.rb, line 293
293:         def should_be_restful(&blk) # :yields: resource
294:           resource = ResourceOptions.new
295:           blk.call(resource)
296:           resource.normalize!(self)
297: 
298:           resource.formats.each do |format|
299:             resource.actions.each do |action|
300:               if self.respond_to? "make_#{action}_#{format}_tests""make_#{action}_#{format}_tests"
301:                 self.send("make_#{action}_#{format}_tests""make_#{action}_#{format}_tests", resource) 
302:               else
303:                 should "test #{action} #{format}" do
304:                   flunk "Test for #{action} as #{format} not implemented"
305:                 end
306:               end
307:             end
308:           end
309:         end

Test macros

Public Instance methods

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

Example:

  should_assign_to :user, :posts

[Source]

     # File lib/shoulda/controller_tests/controller_tests.rb, line 345
345:         def should_assign_to(*names)
346:           names.each do |name|
347:             should "assign @#{name}" do
348:               assert assigns(name.to_sym), "The action isn't assigning to @#{name}"
349:             end
350:           end
351:         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/controller_tests/controller_tests.rb, line 359
359:         def should_not_assign_to(*names)
360:           names.each do |name|
361:             should "not assign to @#{name}" do
362:               assert !assigns(name.to_sym), "@#{name} was visible"
363:             end
364:           end
365:         end

Macro that creates a test asserting that the flash is empty. Same as @should_set_the_flash_to nil@

[Source]

     # File lib/shoulda/controller_tests/controller_tests.rb, line 335
335:         def should_not_set_the_flash
336:           should_set_the_flash_to nil
337:         end

Macro that creates a test asserting that the controller returned a redirect to the given path. The given string is evaled to produce the resulting redirect path. All of the instance variables set by the controller are available to the evaled string. Example:

  should_redirect_to '"/"'
  should_redirect_to "users_url(@user)"

[Source]

     # File lib/shoulda/controller_tests/controller_tests.rb, line 394
394:         def should_redirect_to(url)
395:           should "redirect to #{url.inspect}" do
396:             instantiate_variables_from_assigns do
397:               assert_redirected_to eval(url, self.send(:binding), __FILE__, __LINE__)
398:             end
399:           end
400:         end

Macro that creates a test asserting that the rendered view contains a <form> element.

[Source]

     # File lib/shoulda/controller_tests/controller_tests.rb, line 403
403:         def should_render_a_form
404:           should "display a form" do
405:             assert_select "form", true, "The template doesn't contain a <form> element"            
406:           end
407:         end

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

  should_render_template :new

[Source]

     # File lib/shoulda/controller_tests/controller_tests.rb, line 381
381:         def should_render_template(template)
382:           should "render template #{template.inspect}" do            
383:             assert_template template.to_s
384:           end
385:         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/controller_tests/controller_tests.rb, line 371
371:         def should_respond_with(response)
372:           should "respond with #{response}" do
373:             assert_response response
374:           end
375:         end

Macro that creates a test asserting that the flash contains the given value. val can be a String, a Regex, or nil (indicating that the flash should not be set)

Example:

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

[Source]

     # File lib/shoulda/controller_tests/controller_tests.rb, line 321
321:         def should_set_the_flash_to(val)
322:           if val
323:             should "have #{val.inspect} in the flash" do
324:               assert_contains flash.values, val, ", Flash: #{flash.inspect}"            
325:             end
326:           else
327:             should "not set the flash" do
328:               assert_equal({}, flash, "Flash was set to:\n#{flash.inspect}")
329:             end
330:           end
331:         end