SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    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 one permalink => 'test-story') and it worked -- no more error.

    For anyone familiar with this book, I'd really appreciate any suggestions!

    Thanks,

    Adam

  2. #2
    SitePoint Evangelist
    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 : <&#37;=
     @story.votes.count if @story and @story.votes
    -%></p>
    Or you can do something like:
    Code:
    <% if @story and @story.votes %>
    <p>There are <%= @story.votes.count -%> stories</p>
    <% else %>
    <p>There are no stories</p>
    <% end %>

  3. #3
    SitePoint Member
    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?

  4. #4
    SitePoint Evangelist
    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
    Doesn't suggest that:
    Code:
    @story == nil
    but rather that:
    Code:
    @story.votes == nil
    In other words this story has no votes. Therefore you need to update you code to catch instances where stories have no votes with:
    Code:
    if @story.votes
    You then need to update your functional tests and fixtures to make sure that no error occurs when you show a story with votes and another without votes. So at least two stories in your fixtures and one voters fixture with the foreign key matching one of the fixture stories. And two tests.

  5. #5
    SitePoint Guru
    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.

  6. #6
    SitePoint Member
    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

  7. #7
    SitePoint Guru
    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'

  8. #8
    SitePoint Member
    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.

  9. #9
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    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.
    You're right Fenrir2. My mistake.

  10. #10
    SitePoint Member
    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

  11. #11
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I gave up on the whole deal.

  12. #12
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,460
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    At first I thought you were talking about Simply Rails 2 until I noticed your yml files were different.
    Quote Originally Posted by SuperFish
    but I still think this book should be withdrawn unless/until it better matches the software.
    From http://www.sitepoint.com/books/rails1/
    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.


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
  •