I bought the book and been following and liking it so far! But then I hit a wall…
Trying to run all the 5 tests (13 assertions) results in the assert_select failing with
1) Failure:
test_should_show_new_form(StoriesControllerTest) [/test/functional/stories_controller_test.rb:17]:
Expected at least 3 elements matching "form p", found 0.
<false> is not true.
I tried googling a bit and only advice I found was to re-create the test and/or new.html.erb file(s) and make sure they’re saved as UTF-8. One was, other wasnt, so I saved them both as UTF-8 but unfortunately this did not help. Any ideas how to get the test to pass?
Thanks for the quick reply Mittineague.
Yes, that’s, well, almost what mine looks like. Just that when my rails created the skeleton file, it used a slightly different notation so I decided to stick with it (and it seemed to work just fine with all other test):
.....
test "should show new form" do
get :new
assert_select "form p", :count => 3
end
.....
So I just decided to move on and hope to fix that test later on. Everything worked just fine, until the next functional tests. Unit tests pass just fine, and all functional tests that don’t include assert_select pass as well. But the one that fails this time is:
.....
test "should show story vote elements" do
get :show, :id => stories(:one)
assert_select 'h2 span#vote_score'
assert_select 'ul#vote_history li', :count => 2
assert_select 'div#vote_form form'
end
.....
with this output:
2) Failure:
test_should_show_story_vote_elements(StoriesControllerTest) [/test/functional/stories_controller_test.rb:37]:
Expected at least 1 element matching "h2 span#vote_score", found 0.
<false> is not true.
Again I’m quite certain there’s nothing wrong in the generated html itself, here’s a snippet from it:
.....
<h2>
<span id="vote_score">
Score: 11
</span>
Some random site
</h2>
.....
There turned out to be a typo in the DOCTYPE declaration of my application.html.erb - a double quote was left out. Solving this also solved the assert_select problem.
Leaving out the entire DOCTYPE declaration also did the trick. This would be a good first test.
I’m puzzled, what does your DOCTYPE look like? I have everything wrapped in quotes. I would include it here, but it has a link and the site is not letting me post.
Sorry, I cannot post my DOCTYPE declaration either.
The point is that assert_select can fail if your views generate HTML that is incorrect in some way. Try simplifying your templates/views until assert_select works again.
As I said, in my case, removing the entire DOCTYPE declaration suddenly made assert_select do its job again, and so I knew that there had to be a mistake in that declaration. In your case it might be some other part of the HTML that is wrong: do all the tags that open, also close ? (You did not misspell a closing tag, for instance?) And what about the quotes?
I had the same problem too, seeing error for ‘p’.
After removing all the DOCTYPE declaration, the problem disappeared.
I will try to put the DOCTYPE back piece by piece to find out what went wrong.