Simply Rails 2 Chapter 6 assert_select

Hi,

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.

This is what my new.html.erb looks like:

<%= error_messages_for 'story' %>
<% form_for @story do |f| %>
  <p>
    name:<br />
    <%= f.text_field :name %>
  </p>
  <p>
    link:<br />
    <%= f.text_field :link %>
  </p>
  <p>
    <%= submit_tag %>
  </p>
<% end %>

And this is the part of the test that fails:

assert_select "form p", :count => 3

Finally, this is what the page source looks like:

 		
		
<form action="/stories" class="new_story" id="new_story" method="post"><div style="margin:0;padding:0"><input name="authenticity_token" type="hidden" value="DDY3863fJMhFT3QypnQMOVegpzgpsmC4n5Qd8REe4Mo=" /></div>
  <p>
    name:<br />
    <input id="story_name" name="story[name]" size="30" type="text" />
  </p>
  <p>
    link:<br />
    <input id="story_link" name="story[link]" size="30" type="text" />

  </p>
  <p>
    <input name="commit" type="submit" value="Save changes" />
  </p>
</form>

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?

I am getting the exact same error. I tried renaming my .html.erb file to .rhtml but no go.

If you figure it out please post. Thanks

SF

Hi zelecta, welcome to the forums,

Does your stories_controller_test.rb file look like

require 'test_helper'

class StoriesControllerTest < ActionController::TestCase
.....
  def test_should_show_new_form
    get :new
    assert_select 'form p', :count => 3
  end

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>
.....

I’m really starting to hate assert_select :frowning:

I’m wondering if you experimented with “scaffolding” and the test is finding those files instead?

No I didnt, from the little I had read about RoR until this book, I remembered that scaffolding is in general a no-no, so I didnt do it.

I had exactly the same problem.

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.

Hope this helps.

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 have discovered a missing double quote in my code but am on other issues now and have not gotten back to it …

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.

Thanks for the help!