SitePoint Sponsor |
|
User Tag List
Results 1 to 12 of 12
-
May 14, 2007, 06:02 #1
- Join Date
- May 2007
- Posts
- 22
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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 onepermalink => 'test-story') and it worked -- no more error.
For anyone familiar with this book, I'd really appreciate any suggestions!
Thanks,
Adam
-
May 14, 2007, 14:30 #2
- Join Date
- Feb 2006
- Location
- Worcs. UK
- Posts
- 404
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You can add an if statement to catch instances where there are no items in the database. For example:
Code:<p>Number of stories : <%= @story.votes.count if @story and @story.votes -%></p>
Code:<% if @story and @story.votes %> <p>There are <%= @story.votes.count -%> stories</p> <% else %> <p>There are no stories</p> <% end %>
-
May 14, 2007, 18:00 #3
- Join Date
- May 2007
- Posts
- 22
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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:
def test_should_show_story_vote_elements
get :show,ermalink => 'my-shiny-weblog'
assert_response :success
end
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/ermalink', :controller => 'story', :action =>'show'
I have created an appropriate fixture for story in stories.yml:
first:
id: 1
name: My shiny weblog
link: http://poocs.net/
permalink: my-shiny-weblog
The error comes up again and again as follows:
1) Error:
test_should_show_show(StoryControllerTest):
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
1: <h2>
2: <span id="vote_score">
3: Score: <%= @story.votes.count %>
4: </span>
5: <%= @story.name %>
6: </h2>
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.
Suggestions?
-
May 16, 2007, 04:50 #4
- Join Date
- Feb 2006
- Location
- Worcs. UK
- Posts
- 404
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Do you have a fixture for votes, Does the fixture votes include one for the test story and is your test set to load the votes fixture?
The error occurred while evaluating nil.votes
Code:@story == nil
Code:@story.votes == nil
Code:if @story.votes
-
May 17, 2007, 09:02 #5
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It does suggest that @story == nil. If @story were non-nil, and @story.votes == nil, then you would have seen this error: "The error occurred while evaluating nil.count".
Please post your controller code.
-
May 18, 2007, 19:22 #6
- Join Date
- May 2007
- Posts
- 22
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thank you for your clarification. My controller code (from story_controller.rb) is as follows:
Code Ruby:class StoryController < ApplicationController def index @story = Story.find(:first, :order => 'RAND()') end def new @story = Story.new(params[:story]) if request.post? and @story.save flash[:notice] = 'Story submission succeeded' redirect_to :action => 'index' end end def show @story = Story.find_by_permalink(params[:permalink]) end def vote @story = Story.find(params[:id]) @story.votes.create respond_to do |wants| wants.html { redirect_to :action => 'show', :id => @story.permalink } wants.js { render } end end end
-
May 19, 2007, 03:19 #7
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Try this in the test:
Code Ruby:get :show, 'permalink' => 'my-shiny-weblog'
-
May 20, 2007, 18:14 #8
- Join Date
- May 2007
- Posts
- 22
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'm not sure what changed, but I did this test and got a new, but related, error message:
Code Ruby:1) Error: test_should_show_story_vote_elements(StoryControllerTest): ActionView::TemplateError: You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.to_formatted_s On line #1 of app/views/story/_vote.rhtml 1: <li><%= vote.created_at.to_formatted_s(:short) %></li>
This is strange, since now it suggests that vote is indeed nil. But I have two fixtures -- one for stories and one for votes -- and they are set up so that vote should not be nil. There are 2 votes for 'my-shiny-weblog'
stories.yml looks like this:
Code SQL:FIRST: id: 1 name: My shiny weblog link: [url]http://poocs.net/[/url] permalink: my-shiny-weblog another: id: 2 name: SitePoint Forums link: [url]http://www.sitepoint.com/forums/[/url] permalink: sitepoint-forums
and votes.yml like this:
Code SQL:FIRST: id: 1 story_id: 1 created_at: <% TIME.now.to_s(:db) %> SECOND: id: 2 story_id: 1 created_at: <% TIME.now.to_s(:db) %>
In other words, there should be two votes associated with my-shiny-weblog.
-
May 23, 2007, 04:28 #9
- Join Date
- Feb 2006
- Location
- Worcs. UK
- Posts
- 404
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Jul 11, 2009, 21:05 #10
- Join Date
- Jul 2009
- Posts
- 8
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I am getting nil for @story.
I am finding these glitches in the samples to be annoying at this point. I have spent hours googling up patches to various snags in what should be a straightforward tutorial. I am aware that the problem lies in the fact that Rails changed, but I still think this book should be withdrawn unless/until it better matches the software.
Anyway, if someone knows why that tag is nil, I'd like to know. Thanks
SF
-
Jul 22, 2009, 20:28 #11
- Join Date
- Jul 2009
- Posts
- 8
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I gave up on the whole deal.
-
Jul 30, 2009, 21:33 #12
- Join Date
- Jul 2005
- Location
- West Springfield, Massachusetts
- Posts
- 17,290
- Mentioned
- 198 Post(s)
- Tagged
- 3 Thread(s)
At first I thought you were talking about Simply Rails 2 until I noticed your yml files were different.
Originally Posted by SuperFish
How To Order This Book
Build Your Own Ruby on Rails Web Applications
Unfortunately, this book is no longer available!
The 1st edition of Build Your Own Ruby on Rails Web Applications was a fine book but we replaced it with a new and updated edition in May 2008.
We don’t sell the old version now, but check out the new edition if you're looking for an updated book along the same lines.Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
Bookmarks