SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simply Rails 2, Chapter 7, testing and assertions

    On page 247 of the eBook of Simply Rails 2, I only get 25 assertions instead of 26 when I run my test.

    However, I don't get any failures or errors.

    Is the book wrong, or did I do something wrong? Thoughts?

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,255
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    Hi rumblestrut, welcome to the forums,
    I copied/pasted from the book as I went along rather than use the files in the code archive, which I use(d) only for reference. At the end of chapter 7 for functional tests, I got
    10 tests, 26 assertions. 0 failures, 0 errors
    5 from votes_controller_test and 17 from stories_controller_test
    That adds up to 22 assertions by my count, so I don't know where the other 4 come from.

    If I test only the votes_controller_test I get
    3 tests, 6 assertions, 0 failures, 0 errors
    and when I test only the stories_controller_test I get
    7 tests, 77 assertions, 0 failures, 0 errors

    So I really have know idea where the "extras" come from. I'm guessing they come from "core" tests somehow.

  3. #3
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm... well, I suppose them main thing is to not have errors or failures, right?

    I love Rails, but writing tests is almost like another language in itself. Oddball stuff if you ask me. Still, pretty interesting. Command line tests for a view? That just blows my mind.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,255
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    Sorry if I got a bit sidetracked about "extra" assertions.
    Quote Originally Posted by rumblestrut
    I suppose them main thing is to not have errors or failures, right?
    Not exactly, the main thing is for the testing to be adequately thorough and not have errors or failures.

    The point I should have made more clearly is that you should have 5 "countable" assertions in your votes_controller_test.rb file and 17 in your stories_controller_test.rb file. If not, there's a chance you missed one. If you do have 5 and 17, then maybe you have different versions of ruby/rails/etc and it is only an "extra" one that is missing.

  5. #5
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The number of tests should match the number of methods starting with the key text 'test_' and the number of assertions matches the number of times an assertion statement is run. So a test containing:
    Code:
    def test_one
      assert_equal a, b
      assert_equal b, c
    end
    
    def test_two
      assert_equal c, a
    end
    Will result in two tests and three assertions if the test is successful.

    If you are getting a different number of assertions to the number in the book and no errors or failures you either have a undetected error in your test system or your test code is different to that in the book and you have one less assertion statement. Of the two, I think the latter is more likely.

    By the way if you get a failure, the test stops. So if the first assertion in test_one failed, the system would not go on to the second assertion. The result would then be two tests, two assertions, one failure (assuming there was no failure in test_two. So if you have a failure, the number of assertions in the summary report may not match the number of assertions statements in the test.

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,255
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    I was able to figure out how to run each test file separately. I needed to make some code changes
    Code Ruby:
    #require 'test_helper'
    # above works OK for rake but not for ruby, below works for both
    require File.dirname(__FILE__) + '/../test_helper'
    and the proper command line syntax is
    \shovell>ruby test/functional/*_controller_test.rb
    The results make much more sense than when I removed a file and did
    \shovell>rake test:functionals

    votes_controller_test.rb:
    3 tests, 6 assertions, 0 failures, 0 errors
    stories_controller_test.rb:
    7 tests, 20 assertions, 0 failures, 0 errors

    By comparing with different versions of the files from chapter 6, I was able to find some "2 for 1" assertions. One is in the unit\story_test.rb file
    Code Ruby:
    assert_equal [ votes(:one), votes(:two) ], stories(:one).votes
    which makes sense, the assertion is run twice, once for votes(:one) and once for votes(:two)

    By temporarily commenting out assertions in the functional tests, I was able to find 4 "2 for 1"s
    Code Ruby:
    assert_select 'form p', :count => 3
    assert_redirected_to stories_path
    assert_select 'ul#vote_history li', :count => 2
    assert_redirected_to story_path(stories(:two))
    the assert_select's running twice, once for the "selector" and once for the :count=>
    I don't clearly understand why the assert_redirected_to's assert twice, unless it counts the initial page as 1 and the redirected to page as the other.

    Long story short, one of your functional tests is missing an assertion.

    Try comparing them with the files in the code archive's, chapter 7 folder
    27-stories_controller_test.rb
    30-votes_controller_test.rb


Tags for this Thread

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
  •