Module: ThoughtBot::Shoulda::Controller::ClassMethods
- Defined in:
- lib/shoulda/controller_tests/controller_tests.rb
Overview
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.
Defined Under Namespace
Classes: ResourceOptions
Constant Summary collapse
- VALID_FORMATS =
Formats tested by #should_be_restful. Defaults to [:html, :xml]
Dir.glob(File.join(File.dirname(__FILE__), 'formats', '*.rb')).map { |f| File.basename(f, '.rb') }.map(&:to_sym)
- VALID_ACTIONS =
Actions tested by #should_be_restful
[:index, :show, :new, :edit, :create, :update, :destroy]
Instance Method Summary collapse
-
#should_assign_to(name) ⇒ Object
Macro that creates a test asserting that the controller assigned to @name.
-
#should_be_restful(&blk) ⇒ Object
:section: should_be_restful Generates a full suite of tests for a restful controller.
-
#should_not_assign_to(name) ⇒ Object
Macro that creates a test asserting that the controller did not assign to @name.
-
#should_not_set_the_flash ⇒ Object
Macro that creates a test asserting that the flash is empty.
-
#should_redirect_to(url) ⇒ Object
Macro that creates a test asserting that the controller returned a redirect to the given path.
-
#should_render_a_form ⇒ Object
Macro that creates a test asserting that the rendered view contains a <form> element.
-
#should_render_template(template) ⇒ Object
Macro that creates a test asserting that the controller rendered the given template.
-
#should_respond_with(response) ⇒ Object
Macro that creates a test asserting that the controller responded with a ‘response’ status code.
-
#should_set_the_flash_to(val) ⇒ Object
Macro that creates a test asserting that the flash contains the given value.
Instance Method Details
#should_assign_to(name) ⇒ Object
Macro that creates a test asserting that the controller assigned to @name
Example:
should_assign_to :user
344 345 346 347 348 |
# File 'lib/shoulda/controller_tests/controller_tests.rb', line 344 def should_assign_to(name) should "assign @#{name}" do assert assigns(name.to_sym), "The action isn't assigning to @#{name}" end end |
#should_be_restful(&blk) ⇒ Object
:section: 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.
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/shoulda/controller_tests/controller_tests.rb', line 293 def should_be_restful(&blk) # :yields: resource resource = ResourceOptions.new blk.call(resource) resource.normalize!(self) resource.formats.each do |format| resource.actions.each do |action| if self.respond_to? :"make_#{action}_#{format}_tests" self.send(:"make_#{action}_#{format}_tests", resource) else should "test #{action} #{format}" do flunk "Test for #{action} as #{format} not implemented" end end end end end |
#should_not_assign_to(name) ⇒ Object
Macro that creates a test asserting that the controller did not assign to @name
Example:
should_not_assign_to :user
355 356 357 358 359 |
# File 'lib/shoulda/controller_tests/controller_tests.rb', line 355 def should_not_assign_to(name) should "not assign to @#{name}" do assert !assigns(name.to_sym), "@#{name} was visible" end end |
#should_not_set_the_flash ⇒ Object
Macro that creates a test asserting that the flash is empty. Same as
335 336 337 |
# File 'lib/shoulda/controller_tests/controller_tests.rb', line 335 def should_not_set_the_flash should_set_the_flash_to nil end |
#should_redirect_to(url) ⇒ Object
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)"
388 389 390 391 392 393 394 |
# File 'lib/shoulda/controller_tests/controller_tests.rb', line 388 def should_redirect_to(url) should "redirect to #{url.inspect}" do instantiate_variables_from_assigns do assert_redirected_to eval(url, self.send(:binding), __FILE__, __LINE__) end end end |
#should_render_a_form ⇒ Object
Macro that creates a test asserting that the rendered view contains a <form> element.
397 398 399 400 401 |
# File 'lib/shoulda/controller_tests/controller_tests.rb', line 397 def should_render_a_form should "display a form" do assert_select "form", true, "The template doesn't contain a <form> element" end end |
#should_render_template(template) ⇒ Object
Macro that creates a test asserting that the controller rendered the given template. Example:
should_render_template :new
375 376 377 378 379 |
# File 'lib/shoulda/controller_tests/controller_tests.rb', line 375 def should_render_template(template) should "render template #{template.inspect}" do assert_template template.to_s end end |
#should_respond_with(response) ⇒ Object
Macro that creates a test asserting that the controller responded with a ‘response’ status code. Example:
should_respond_with :success
365 366 367 368 369 |
# File 'lib/shoulda/controller_tests/controller_tests.rb', line 365 def should_respond_with(response) should "respond with #{response}" do assert_response response end end |
#should_set_the_flash_to(val) ⇒ Object
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
321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/shoulda/controller_tests/controller_tests.rb', line 321 def should_set_the_flash_to(val) if val should "have #{val.inspect} in the flash" do assert_contains flash.values, val, ", Flash: #{flash.inspect}" end else should "not set the flash" do assert_equal({}, flash, "Flash was set to:\n#{flash.inspect}") end end end |