By using the macro helpers you can quickly and easily create concise and easy to read test suites.
This code segment:
describe UsersController, "on GET to show with a valid id" do before(:each) do get :show, :id => User.first.to_param end it { should assign_to(:user) } it { should respond_with(:success) } it { should render_template(:show) } it { should not_set_the_flash) } it "should do something else really cool" do assigns[:user].id.should == 1 end end
Would produce 5 tests for the show action
Ensures that the controller assigned to the named instance variable.
Options:
with_kind_of - The expected class of the instance variable being checked.
with - The value that should be assigned.
Example:
it { should assign_to(:user) } it { should_not assign_to(:user) } it { should assign_to(:user).with_kind_of(User) } it { should assign_to(:user).with(@user) }
# File lib/shoulda/action_controller/matchers/assign_to_matcher.rb, line 18 def assign_to(variable) AssignToMatcher.new(variable) end
Ensures that filter_parameter_logging is set for the specified key.
Example:
it { should filter_param(:password) }
# File lib/shoulda/action_controller/matchers/filter_param_matcher.rb, line 10 def filter_param(key) FilterParamMatcher.new(key) end
Ensures a controller redirected to the given url.
Example:
it { should redirect_to('http://somewhere.com') } it { should redirect_to(users_path) }
# File lib/shoulda/action_controller/matchers/redirect_to_matcher.rb, line 11 def redirect_to(url_or_description, &block) RedirectToMatcher.new(url_or_description, self, &block) end
Ensures a controller rendered the given template.
Example:
it { should render_template(:show) }
# File lib/shoulda/action_controller/matchers/render_template_matcher.rb, line 10 def render_template(template) RenderTemplateMatcher.new(template, self) end
Ensures that the controller rendered with the given layout.
Example:
it { should render_with_layout } it { should render_with_layout(:special) } it { should_not render_with_layout }
# File lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb, line 12 def render_with_layout(expected_layout = nil) RenderWithLayout.new(expected_layout) end
Ensures a controller responded with expected ‘response’ status code.
You can pass an explicit status number like 200, 301, 404, 500 or its symbolic equivalent :success, :redirect, :missing, :error. See ActionController::StatusCodes for a full list.
Example:
it { should respond_with(:success) } it { should respond_with(:redirect) } it { should respond_with(:missing) } it { should respond_with(:error) } it { should respond_with(501) }
# File lib/shoulda/action_controller/matchers/respond_with_matcher.rb, line 18 def respond_with(status) RespondWithMatcher.new(status) end
Ensures a controller responded with expected ‘response’ content type.
You can pass an explicit content type such as ‘application/rss+xml’ or its symbolic equivalent :rss or a regular expression such as /rss/
Example:
it { should respond_with_content_type(:xml) } it { should respond_with_content_type(:csv) } it { should respond_with_content_type(:atom) } it { should respond_with_content_type(:yaml) } it { should respond_with_content_type(:text) } it { should respond_with_content_type('application/rss+xml') } it { should respond_with_content_type(/json/) }
# File lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb, line 20 def respond_with_content_type(content_type) RespondWithContentTypeMatcher.new(content_type) end
Ensures that requesting path using method routes to options.
If you don’t specify a controller, it will use the controller from the example group.
to_param is called on the options given.
Examples:
it { should route(:get, "/posts"). to(:controller => :posts, :action => :index) } it { should route(:get, "/posts/new").to(:action => :new) } it { should route(:post, "/posts").to(:action => :create) } it { should route(:get, "/posts/1").to(:action => :show, :id => 1) } it { should route(:edit, "/posts/1").to(:action => :show, :id => 1) } it { should route(:put, "/posts/1").to(:action => :update, :id => 1) } it { should route(:delete, "/posts/1"). to(:action => :destroy, :id => 1) } it { should route(:get, "/users/1/posts/1"). to(:action => :show, :id => 1, :user_id => 1) }
# File lib/shoulda/action_controller/matchers/route_matcher.rb, line 25 def route(method, path) RouteMatcher.new(method, path, self) end
Ensures that a session key was set to the expected value.
Example:
it { should set_session(:message) } it { should set_session(:user_id).to(@user.id) } it { should_not set_session(:user_id) }
# File lib/shoulda/action_controller/matchers/set_session_matcher.rb, line 12 def set_session(key) SetSessionMatcher.new(key) end
Ensures that the flash contains the given value. Can be a String, a Regexp, or nil (indicating that the flash should not be set).
Example:
it { should set_the_flash } it { should set_the_flash.to("Thank you for placing this order.") } it { should set_the_flash.to(/created/i) } it { should_not set_the_flash }
# File lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb, line 14 def set_the_flash SetTheFlashMatcher.new end
Generated with the Darkfish Rdoc Generator 2.