SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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
    Code:
      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:
    HTML Code:
    <%= 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:
    Code Ruby:
    assert_select "form p", :count => 3

    Finally, this is what the page source looks like:
    HTML Code:
    		
    <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?

  2. #2
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    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

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,227
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    Hi zelecta, welcome to the forums,

    Does your stories_controller_test.rb file look like
    Code Ruby:
    require 'test_helper'
     
    class StoriesControllerTest < ActionController::TestCase
    .....
      def test_should_show_new_form
        get :new
        assert_select 'form p', :count => 3
      end

  4. #4
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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):
    Code Ruby:
    .....
    test "should show new form" do
      	get :new
     	assert_select "form p", :count => 3 
    end
    .....

  5. #5
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code Ruby:
    .....
    	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:
    Code:
      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:
    HTML Code:
    .....
    <h2>
    	<span id="vote_score">
    		Score: 11
    	</span>
    	Some random site
    </h2>
    .....
    I'm really starting to hate assert_select

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,227
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    I'm wondering if you experimented with "scaffolding" and the test is finding those files instead?

  7. #7
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  9. #9
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kcoolsae View Post
    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.

  10. #10
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  11. #11
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have discovered a missing double quote in my code but am on other issues now and have not gotten back to it ...

  12. #12
    SitePoint Member
    Join Date
    Apr 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!



    Quote Originally Posted by kcoolsae View 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?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •