Assert List
- assert boolean
- assert_equal expected, actual
- assert_raise *args
- assert_raises *args, &block
- assert_instance_of klass, object
- assert_nil object
- assert_kind_of klass, object
- assert_respond_to object, method
- assert_match pattern, string
- assert_same expected, actual
- assert_operator object1, operator, object2
- assert_nothing_raised *args
- assert_not_same expected, actual
- assert_not_equal expected, actual
- assert_not_nil object
- assert_no_match regexp, string
- assert_throws expected_symbol, &proc
- assert_nothing_thrown &proc
- assert_in_delta expected_float, actual_float, delta
- assert_send send_array
- assert_response type
- assert_redirected_to options = {}
- assert_template expected
- assert_recognizes expected_options, path, extras={}
- assert_generates expected_path, options, defaults={}, extras = {}
- assert_routing path, options, defaults={}, extras={}
- assert_tag *opts
- assert_no_tag *opts
- assert_dom_equal expected, actual
- assert_dom_not_equal expected, actual
- assert_valid record
- assert_assigned ivar, value = NOTHING
- • deny_assigned
- assert_content_type type, message = nil
- assert_flash key, content
- assert_image src
- assert_error_on field, type
- assert_field form_action, type, model, column, value = nil
- assert_input form_action, type, name, value = nil
- assert_label form_action, name, include_f = true
- assert_links_to href, content = nil
- • deny_links_to
- assert_multipart_form form_action
- assert_post_form form_action
- assert_select form_action, model, column, options
- assert_submit form_action, value
- assert_tag_in_form form_action, options
- • deny
- assert_empty obj
- • deny_empty
- assert_includes obj, item, message = nil
- • deny_includes
RSpec Assert List
Model validation
@user.should.validate
@user.should.not.validate
or:
@user.should.be.validated
@user.should.not.be.validated
Redirection
response.should.be.redirected # assert_response :redirect
response.should.redirect foo_url(@foo) # assert_redirected_to foo_url(@foo)
should.redirect_to :action => 'show' # because "response" is optional
It's aliased as redirect, redirect_to, redirected and redirected_to
Output verification
Wrapper for assert_select:
get :show
page.should.select "form#user_form" # require the output to have a <form id="user_form">
page.should.select "form#user_form" do |form|
form.should.select "input[type=submit]" # the user_form must include a <input type="submit">
end
HTTP Status
Wrapper for assert_response:
status.should.be :success
status.should.be 200
Which template was rendered?
Wrapper for assert_template:
template.should.be "foo/show.rhtml"
Which layout used?
Wrapper for asserting that a certain layout is used:
layout.should.be "foo"
URL testing
get :show, :user_id => 1
url.should.be "/users/1"
url.should.be :controller => "users", :action => "show", :user_id => 1
Difference
Wrapper for assert_difference:
Article.should.differ(:count).by(2) { blah }
Routing to set of url options from a given path (i.e.,#assert_generates)
assert_generates "/users/1", :controller => "users", :action => "show", :id => "1"
becomes
{:controller => "users", :action => "show", :id => "1"}.should.route_to "/users/1"
Routing from a set of url options to an expected path (i.e.,#assert_recognizes)
assert_recognizes({:controller => "users", :action => "show", :id => "1"}, {:path => "/users/1", :method => :get})
becomes
{:path => "/users/1", :method => :get}.should.route_from :controller => "users", :action => "show", :id =>"1"
Setup controller for testing + example usage
context "A guest" do
use_controller FooController
specify "isn't allowed to foo" do
post :create, :foo => 'bar'
flash[:error].should.not.be.blank
response.should.redirect :controller => 'front', :action => 'show'
assigns(:foo).errors.on(:bar).should.not.be.blank
assigns(:foo).should.not.validate
follow_redirect
page.should.select "#errors", :text => /#{flash[:error]}/
end
end
context "Tuxie" do
setup do
use_controller FooController
login_as :tuxie
end
specify "may foo" do
post :create, :foo => 'bar'
flash[:notice].should.not.be.blank
assigns(:foo).errors.should.be.empty
assigns(:foo).should.validate
should.redirect_to :action => 'show'
follow_redirect
page.should.select "#notice", :text => /#{flash[:notice]}/
end
end
Misc
page, response and request are just aliases for self (the TestCase) so the following are functionally identical:
page.should.redirect
response.should.redirect
request.should.redirect
output, body, html and xhtml return @response.body so the following lines are identical:
output.should.match /foo/
html.should.match /foo/
xhtml.should.match /foo/
body.should.match /foo/
If an object respond to #should_equal, that method will be used instead of assert_equal
when using foo.should.equal(bar) or foo.should.be(bar)
foo.should.not.equal and foo.should.not.be look for #should_not_equal