RoR 2.0 Chapter 9: rake test

Running into some problems running the rake test for chapter 9. I hope someone can help.

My error message is:
Finished in 1.515 seconds.

  1. Failure:
    Expected at most 1 element matching “div#stories_voted_on div.story”, found 0.
    <false> is not true.

  2. Failure:
    Expected at least 2 elements matching “div#stories_submitted div.story”, found 0.
    <false> is not true.

I have spent a couple days trying to figure it out myself but am caught.

I confused myself between the /app/views/STORIES/show.html.erb and the /app/views/USERS/show.html.erb that are covered on page 333-334.

If anyone would like to show me how to interpret the error message so i can figure it out in the future that would be helpful. I solved this by just going back through the book page by page which won’t be an option if I ever venture out on my own some day.



OK, I’ll try, but there’s not much here to go on. (I don’t have the second edition of the book, so I can’t review it for more info.)

The first principle in writing tests is to name the tests in such a way you can identify them completely.

Another good idea (which all too few books mention) is to make use of the “message” argument in the assertion. For example:

assert_equal 2, stories.count, "The number of stories wasn't what it should have been in the user test"

If the test fails, that message will be included in the results, giving you more information about the test that just failed. By filling the message argument with a complete explanation of your test, you give yourself a start on finding the problem.

Note: testing only tells you that a test failed, it doesn’t tell you why. It just says “Hey, I didn’t get the result you told me I should get here! You’d better take a look and see what’s happening.”

OK, that just tells you two tests failed. There’s no practical way of accurately determining which ones, so don’t bother. The key for this line is whether the letter is an F or and E (E means there was an error trying to run the test, so the pass/fail results cannot be determined, and you need to fix that problem before the test can be run).

This group tells you the test named “test_should_show_stories_voted_on” in the file “users_controller_test.rb” failed. It expected to see one matching element, but found none. That means the particular element you told it to expect wasn’t in the view you were testing, so you need to go back to the view to find out why.

The final line, “<false> is not true,” simply means the assertion failed, which is why the flag wa raised.

This is the same as the previous failure, except in this case it expected 2 of those particular elements, and didn’t find any.

When you’re just getting started and you’re working off code from a book, you did just the right thing: you went back to the book first and rechecked what you did. You didn’t necessarily understand the test you wrote, you copied it out of the book.

The next step is to understand the test and why it failed, which I think you do because you mentioned you confused two views.

When you’re out on your own this kind of thing won’t happen as frequently. You won’t be able to write a test without understanding what it is you’re looking for (obviously, since there’s nothing to copy the test from). So the limited information you get from the testing system is enough to help you find it.

Everyone passes through the “cargo cult programmer” stage (where you copy work from other sources and use it without understanding it). The key is to look over the code and come to understand it.

You’re on your way. You’ll trip over a few things, but that’s natural. Anyone who says they didn’t is lying. Just try not to trip over the same thing more than once. ;{>}

i just had the same error so i thought i’d post this here for anyone else who comes across it.

the way i solved this error was to go to a user page on the test server i was running of the program. i looked at the html, searching for the div tags “stories_voted_on” and “stories_submitted”, and lo and behold, i saw them there as “stories voted on” and “stories submitted”. a forgotten underscore was making the error in both cases, an incredibly annoying mistake but pretty easy to fix once you figure out what’s going on.