SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    May 2006
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Chapter 6 controller test problems

    Hi everyone,

    I'm trying to work through Patrick Lenz' book. And am getting stuck on the functional tests. I am running Instant Rails 2.0. I have run into one difference from the book, which was to do with scaffolding, but since the book was only using it for a quick demonstration, then building views directly, I didn't worry about it.

    I built the view and controller the way the book said, ran the unit tests, which all worked perfectly. The differences between my skeleton functional test file and the books were pretty significant. I first tried making the book edits based on the generated skeleton (assuming the 2.0 changes were important) I tried running the rake test: functionals and got a bunch of errors, so I went back and used the book story_controller_test.rb. I've checked everything as much as I could.

    When I run the functional tests, I get an error that says
    Code:
    1) Error: test_should_show_index(StoryControllerTest):
    ActionView::TemplateError: You have a nil object when you didn't expect it!
    The error occured while evaluating nil.name 
        On line #2 of story/index.html.erb
    It shows the only two lines of code in my index.html.erb file which are as follows:
    Code:
    A random link:
    <%= link_to @story.name, @story.link %>
    It then spits out 30-40 lines of ruby/libs/ruby/gems/1.8/gems/* all kinds of problems.

    Any ideas? Are their any global changes that need to be done to accomodate the book to 2.0? I am new to Ruby (hence the book) and assume that any references to .rhtml are now to be changed to .html.erb.

    Here is my story_controller_test.rb file
    Code:
    require File.dirname(__FILE__) + '/../test_helper'
    require 'story_controller'
    
    # Re-raise errors caught by the controller
    class StoryController; def rescue_action(e) raise e end; end
    
    class StoryControllerTest < Test::Unit::TestCase
    	fixtures :stories
    	def setup
    		@controller = StoryController.new
    		@request = ActionController::TestRequest.new
    		@response = ActionController::TestResponse.new
    	end
    	
      def test_should_show_index
        get :index
        assert_response :success
        assert_template 'index'
        assert_not_nil assigns(:story)
      end
    end
    Please let me know if there is more info that you need to give me any guidance. Happy Mother's Day everyone.

    Don

  2. #2
    SitePoint Enthusiast AllTom's Avatar
    Join Date
    Feb 2008
    Location
    Wherever danger lies...
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's saying that your view code is trying to ask nil.name, which here means that @story is nil. That means that the controller is not assigning any value to @story.
    There are some things you shouldn't try to code at home.

  3. #3
    SitePoint Member
    Join Date
    May 2006
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks

    At least I know where to start looking. Thanks.

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please post your controller code it you need further help. The problem is likely in your controller.

  5. #5
    SitePoint Member
    Join Date
    May 2006
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Controller code

    Here is my controller code, I double checked and it is the same as in the book.

    Code:
    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
      
    end
    Last edited by donholloway; May 13, 2008 at 06:09.

  6. #6
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,329
    Mentioned
    191 Post(s)
    Tagged
    4 Thread(s)
    Is there data in your database?
    As AllTom stated, your @story variable is nil. That is being read from the database. Maybe the database is empty.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains


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
  •