SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with stories_controller_test

    I am trying to run functional tests using stories_controller_test as explained in
    Simply Rails 2 p. 186 the code is below. The second test fails. Any ideas why?
    This is the test
    Code:
    require 'test_helper'
    
    class storiesControllerTest < ActionController::TestCase
      def test_should_show_index
        get :index
        assert_response :success
        assert_template 'index'
        assert_not_nil assigns(:story)
      end
      def test_should_show_new
        get :new
        assert_response :success
        assert_template 'new'
        assert_not_nil assigns(:story)
      end
      
      
    end
    This is the error

    Code:
    Started
    .F..
    Finished in 0.074679 seconds.
    
      1) Failure:
    test_should_show_new(StoriesControllerTest) [/test/functional/stories_controller_test.rb:12]:
    Expected response to be a <:success>, but was <302>
    
    4 tests, 6 assertions, 1 failures, 0 errors
    And this is what happens in the terminal when you go to the relevant pages

    Code:
    Processing storiesController#index (for 127.0.0.1 at 2010-01-19 15:21:15) [GET]
      User Load (0.6ms)   SELECT * FROM "users" WHERE ("users"."id" = 1) LIMIT 1
      Entry Load (1.6ms)   SELECT * FROM "stories" 
    Rendering template within layouts/application
    Rendering stories/index
      CACHE (0.0ms)   SELECT * FROM "users" WHERE ("users"."id" = 1) LIMIT 1
    Completed in 44ms (View: 22, DB: 2) | 200 OK [http://localhost/stories/]
    
    
    Processing storiesController#new (for 127.0.0.1 at 2010-01-19 15:22:56) [GET]
      User Load (0.6ms)   SELECT * FROM "users" WHERE ("users"."id" = 1) LIMIT 1
    Rendering template within layouts/application
    Rendering stories/new
      CACHE (0.0ms)   SELECT * FROM "users" WHERE ("users"."id" = 1) LIMIT 1
    Completed in 24ms (View: 5, DB: 1) | 200 OK [http://localhost/stories/new]

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Location
    Cleveland, OH
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you please show the code in your controller?

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    class storiesController < ApplicationController
      before_filter :login_required, :only => [ :new, :create ]
      
    
      def index
        @story = story.find(:all)
      end
    
      def new
        @story = story.new
      end
    
      def show
        @story = story.find(params[:id])
      end
    
      def edit
        @story = story.find(params[:id])
      end
    
      def update
        @story = story.find(params[:id])
        @story.update_attributes(params[:story])
        render :action => 'show'
      end
    
      def create
        if session[:user_id]
        @story = story.new(params[:story])
        @current_user = User.find_by_id(session[:user_id])
        @story.user_id = @current_user.login
              if @story.save
                flash[:notice] = 'story was successfully created.'
              redirect_to(@story) 
              end
        else
          redirect_to stories_path
        end
      end
    
      def destroy
        @story = story.find(params[:id])
        @story.destroy
    
        redirect_to(stories_path)
      end
    end

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Location
    Cleveland, OH
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's my best guess.

    The test results are failing because instead of getting an HTTP 200 response, you are getting HTTP 302. The reason you are getting the 302 (which is the code for redirect) is the before_filter :login_requried. The login_required action checks to see if the user is logged in, if yes, you pass to new action and get the 200 code, but if the user is not logged in you get redirected to the login page (302). If you comment out the before filter and run the test, I think it would pass.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Location
    Cleveland, OH
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just checked the Rails 2 book and the test that include the additional code required to pass with the login/logout functionality enabled on the StoriesController begins on pg 297. It looks like you would need to add one line of code to the test to get it to work.

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks that did the trick. My fault, I created the whole app without looking at the book and never considered that the user tests affected the story tests.


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
  •