Spec::Rails::Expectations::Matchers provides several expectation matchers intended to work with Rails components like models and responses. For example:
response.should redirect_to("some/url") #redirect_to(url) is the matcher.
In addition to those you see below, the arbitrary predicate feature of RSpec makes the following available as well:
response.should be_success #passes if response.success? response.should be_redirect #passes if response.redirect?
Note that many of these matchers are part of a wrapper of assert_select, so the documentation comes straight from that with some slight modifications. assert_select is a Test::Unit extension originally contributed to the Rails community as a plugin by Assaf Arkin and eventually shipped as part of Rails.
For more info on assert_select, see the relevant Rails documentation.
wrapper for assert_select_rjs
see documentation for assert_select_rjs at api.rubyonrails.org/
# File vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/assert_select.rb, line 108 108: def have_rjs(*args, &block) 109: AssertSelect.new(:assert_select_rjs, self, *args, &block) 110: end
wrapper for assert_select with additional support for using css selectors to set expectation on Strings. Use this in helper specs, for example, to set expectations on the results of helper methods.
# in a controller spec response.should have_tag("div", "some text") # in a helper spec (person_address_tag is a method in the helper) person_address_tag.should have_tag("input#person_address")
see documentation for assert_select at api.rubyonrails.org/
# File vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/assert_select.rb, line 76 76: def have_tag(*args, &block) 77: AssertSelect.new(:assert_select, self, *args, &block) 78: end
Accepts a String or a Regexp, matching a String using == and a Regexp using =~.
Use this instead of response.should have_tag() when you either don‘t know or don‘t care where on the page this text appears.
response.should have_text("This is the expected text")
# File vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/have_text.rb, line 49 49: def have_text(text) 50: HaveText.new(text) 51: end
Passes if the response is a redirect to the url, action or controller/action. Useful in controller specs (integration or isolation mode).
response.should redirect_to("path/to/action") response.should redirect_to("http://test.host/path/to/action") response.should redirect_to(:action => 'list')
# File vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/redirect_to.rb, line 107 107: def redirect_to(opts) 108: RedirectTo.new(request, opts) 109: end
Passes if the specified template is rendered by the response. Useful in controller specs (integration or isolation mode).
path can include the controller path or not. It can also include an optional extension (no extension assumes .rhtml).
Note that partials must be spelled with the preceding underscore.
response.should render_template('list') response.should render_template('same_controller/list') response.should render_template('other_controller/list') #rjs response.should render_template('list.rjs') response.should render_template('same_controller/list.rjs') response.should render_template('other_controller/list.rjs') #partials response.should render_template('_a_partial') response.should render_template('same_controller/_a_partial') response.should render_template('other_controller/_a_partial')
# File vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/render_template.rb, line 64 64: def render_template(path) 65: RenderTemplate.new(path.to_s, @controller) 66: end
wrapper for assert_select_email
see documentation for assert_select_email at api.rubyonrails.org/
# File vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/assert_select.rb, line 118 118: def send_email(*args, &block) 119: AssertSelect.new(:assert_select_email, self, *args, &block) 120: end
wrapper for assert_select_encoded
see documentation for assert_select_encoded at api.rubyonrails.org/
# File vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/assert_select.rb, line 125 125: def with_encoded(*args, &block) 126: should AssertSelect.new(:assert_select_encoded, self, *args, &block) 127: end
wrapper for a nested assert_select
response.should have_tag("div#form") do with_tag("input#person_name[name=?]", "person[name]") end
see documentation for assert_select at api.rubyonrails.org/
# File vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/assert_select.rb, line 87 87: def with_tag(*args, &block) 88: should have_tag(*args, &block) 89: end
wrapper for a nested assert_select with false
response.should have_tag("div#1") do without_tag("span", "some text that shouldn't be there") end
see documentation for assert_select at api.rubyonrails.org/
# File vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/assert_select.rb, line 98 98: def without_tag(*args, &block) 99: should_not have_tag(*args, &block) 100: end