I’m having a problem with one of my tests in RSpec. The test in question is supposed to test whether there is more than one post on the index page, by checking for the existence of more than one <article class="post"> tag on the page. My current implementation looks like this:
it "should have more than one post" do
get :index
response.should have_selector("article", :class => "post", :count => 2)
end
For some reason this test fails, despite showing multiple instances of the <article class="post"> tag in the debug output. The specific error it give is
Failures:
1) PostsController GET 'index' should have more than one posts
Failure/Error: response.should have_selector("article", :class => "post", :count => 2)
expected following output to contain a <article class='post'/> tag:
Note that if I remove the :count => 2 option the test passes without incident.
Does anyone know why this test is failing?
I’m guessing you are using Capybara under RSpec here or Webrat? With the count option your test is asserting that the response contains exactly 2 posts, rather than at least 2. Does the minimum option do what you want?
See https://github.com/jnicklas/capybara/blob/master/lib/capybara/node/matchers.rb and http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Matchers#has_selector%3F-instance_method