Problem with testing in SitePoint book tutorial
I have a question about a SitePoint book by Patrick Lenz called Build Your Own Ruby on Rails Web Applications.
I think it's a great book, but I'm having some trouble with the controller tests in Chapter 7.
I get an error on test_should_show_story_vote_elements. The problem seems to be with getting the show page. In looking over what I've created based on the book's tutorial, this doesn't seem that surprising. If I go to http://localhost:3000/story/show/, I get an error message because the @story variable equals nil. The page show.rhtml requires that @story refer to something (it shows a score, which looks for @story.votes.count). But what if no story has been specified? You get a nil object and an error.
Any suggestions? Am I missing something?
Next, I kept getting an error regarding test_should_add_story. It looked to me as though the story was having trouble being added because no permalink attribute was included. I included one (:permalink => 'test-story') and it worked -- no more error.
For anyone familiar with this book, I'd really appreciate any suggestions!
A routing problem, possibly?
Thanks for that suggestion -- it does work for part of my problem.
However, the main challenge I'm having is getting a test to succeed which should not depend on doing any if/else checking. This is the test that fails:
get :show, :permalink => 'my-shiny-weblog'
This should successfully connect to story/show/my-shiny-weblog but it doesn't.
Why? I have routed appropriately in config/routes:
map.story 'story/show/:permalink', :controller => 'story', :action =>'show'
I have created an appropriate fixture for story in stories.yml:
name: My shiny weblog
The error comes up again and again as follows:
ActionView::TemplateError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.votes
On line #3 of app/views/story/show.rhtml
2: <span id="vote_score">
3: Score: <%= @story.votes.count %>
5: <%= @story.name %>
For some reason, the server doesn't know that @story refers to my-shiny-weblog. This is strange, since I can point my browser to http://localhost:3000/story/show/my-permalink-here and it works every time.