Module: ThoughtBot::Shoulda::Controller::Macros
- Included in:
- Test::Unit::TestCase
- Defined in:
- lib/shoulda/controller/macros.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.
Instance Method Summary collapse
-
#should_assign_to(*names) ⇒ Object
Macro that creates a test asserting that the controller assigned to each of the named instance variable(s).
-
#should_be_restful(&blk) ⇒ Object
:section: should_be_restful Generates a full suite of tests for a restful controller.
-
#should_not_assign_to(*names) ⇒ Object
Macro that creates a test asserting that the controller did not assign to any of the named instance variable(s).
-
#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
:section: Test macros Macro that creates a test asserting that the flash contains the given value.
Instance Method Details
#should_assign_to(*names) ⇒ Object
Macro that creates a test asserting that the controller assigned to each of the named instance variable(s).
Example:
should_assign_to :user, :posts
105 106 107 108 109 110 111 |
# File 'lib/shoulda/controller/macros.rb', line 105 def should_assign_to(*names) names.each do |name| should "assign @#{name}" do assert assigns(name.to_sym), "The action isn't assigning to @#{name}" end 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.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/shoulda/controller/macros.rb', line 54 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(*names) ⇒ Object
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
119 120 121 122 123 124 125 |
# File 'lib/shoulda/controller/macros.rb', line 119 def should_not_assign_to(*names) names.each do |name| should "not assign to @#{name}" do assert !assigns(name.to_sym), "@#{name} was visible" end end end |
#should_not_set_the_flash ⇒ Object
Macro that creates a test asserting that the flash is empty. Same as
95 96 97 |
# File 'lib/shoulda/controller/macros.rb', line 95 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)"
154 155 156 157 158 159 160 |
# File 'lib/shoulda/controller/macros.rb', line 154 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.
163 164 165 166 167 |
# File 'lib/shoulda/controller/macros.rb', line 163 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
141 142 143 144 145 |
# File 'lib/shoulda/controller/macros.rb', line 141 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
131 132 133 134 135 |
# File 'lib/shoulda/controller/macros.rb', line 131 def should_respond_with(response) should "respond with #{response}" do assert_response response end end |
#should_set_the_flash_to(val) ⇒ Object
:section: Test macros 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
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/shoulda/controller/macros.rb', line 81 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 |