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?
| SitePoint Sponsor |
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?


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


Sorry if I got a bit sidetracked about "extra" assertions.
Not exactly, the main thing is for the testing to be adequately thorough and not have errors or failures.Originally Posted by rumblestrut
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.
Last edited by Mittineague; Sep 4, 2008 at 21:29.



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:
Will result in two tests and three assertions if the test is successful.Code:def test_one assert_equal a, b assert_equal b, c end def test_two assert_equal c, a end
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.


I was able to figure out how to run each test file separately. I needed to make some code changes
and the proper command line syntax isCode Ruby:#require 'test_helper' # above works OK for rake but not for ruby, below works for both require File.dirname(__FILE__) + '/../test_helper'
\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
which makes sense, the assertion is run twice, once for votes(:one) and once for votes(:two)Code Ruby:assert_equal [ votes(:one), votes(:two) ], stories(:one).votes
By temporarily commenting out assertions in the functional tests, I was able to find 4 "2 for 1"s
the assert_select's running twice, once for the "selector" and once for the :count=>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))
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
Last edited by Mittineague; Sep 5, 2008 at 10:13.
Bookmarks