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:
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) }
Ensures that filter_parameter_logging is set for the specified key.
Example:
it { should filter_param(:password) }
Ensures a controller redirected to the given url.
Example:
it { should redirect_to('http://somewhere.com') } it { should redirect_to(users_path) }
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 }
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) }
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/) }
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) }
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) }
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 set_the_flash.to(/logged in/i).now } it { should_not set_the_flash }