SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Houston, TX
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Chapter 08 Functional Test Errors

    I noticed that some people had trouble with unit and functional tests in chapter 6, but nothing came up re: chapter 8. I copied in the code, checked it twice, and just copied the code_archive files, all with the same result. These are the errors I'm getting:

    Code:
      1) Failure:
    test_should_redirect_after_login_with_return_url(AccountControllerTest)
        [/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/assertions/response_assertions.rb:86:in `assert_redirected_to'
         /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/assertions/response_assertions.rb:35:in `assert_redirected_to'
         ./test/functional/account_controller_test.rb:39:in `test_should_redirect_after_login_with_return_url']:
    response is not a redirection to all of the options supplied (redirection is <{"controller"=>"story"}>), difference: <{"action"=>"new"}>
    
      2) Error:
    test_should_add_story(StoryControllerTest):
    NoMethodError: You have a nil object when you didn't expect it!
    You might have expected an instance of ActiveRecord::Base.
    The error occurred while evaluating nil.new_record?
        ./test/functional/story_controller_test.rb:40:in `test_should_add_story'
    
      3) Failure:
    test_should_indicate_logged_in_user(StoryControllerTest) [./test/functional/story_controller_test.rb:99]:
    <#<User:0x33aaa90
     @attributes=
      {"name"=>"Patrick Lenz",
       "id"=>"1",
       "password"=>"sekrit",
       "login"=>"patrick",
       "email"=>"patrick@lenz.sh"}>> expected but was
    <nil>.
    
      4) Error:
    test_should_reject_missing_story_attribute(StoryControllerTest):
    NoMethodError: You have a nil object when you didn't expect it!
    You might have expected an instance of ActiveRecord::Base.
    The error occurred while evaluating nil.errors
        ./test/functional/story_controller_test.rb:47:in `test_should_reject_missing_story_attribute'
    
      5) Failure:
    test_should_show_new(StoryControllerTest)
        [/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/assertions/response_assertions.rb:26:in `assert_response'
         /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/assertions/response_assertions.rb:18:in `assert_response'
         ./test/functional/story_controller_test.rb:25:in `test_should_show_new']:
    Expected response to be a <:success>, but was <302>
    
      6) Failure:
    test_should_show_new_form(StoryControllerTest)
        [/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/assertions/selector_assertions.rb:281:in `assert_select'
         ./test/functional/story_controller_test.rb:32:in `test_should_show_new_form']:
    Expected at least 3 elements, found 0.
    <false> is not true.
    
      7) Error:
    test_should_store_user_with_story(StoryControllerTest):
    NoMethodError: You have a nil object when you didn't expect it!
    The error occurred while evaluating nil.user
        ./test/functional/story_controller_test.rb:114:in `test_should_store_user_with_story'
    
    21 tests, 44 assertions, 4 failures, 3 errors
    rake aborted!
    Command failed with status (1): [/usr/local/bin/ruby -Ilib:test "/usr/local...]
    I'm positive that it is a problem with the login functionality, but I can't figure out why the first part of the account_controller_test.rb has no login problems, while the 'test_should_redirect_after_login_with_return_url' test and all the tests in the story_controller_test.rb using the protected methods all fail. Any thoughts? Has anyone else had this problem?

  2. #2
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Houston, TX
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Addendum...

    I'm hoping a little more info might help. I'm running on Mac OS X 10.4 with ruby 1.8.5-p12 and rubygems 0.9.0. Also, these are the code sections in particular that are giving me grief:

    account_controller_test.rb:
    Code:
      ...
      def test_should_redirect_after_login_with_return_url
        post :login, { :login => 'patrick', :password => 'sekrit' },
            :return_to => '/story/new'
        assert_redirected_to '/story/new'
      end
      ...
    story_controller_test.rb:
    Code:
      ...
      def test_should_show_new
        get_with_user :new
        assert_response :success
        assert_template 'new'
        assert_not_nil assigns(:story)
      end
      
      def test_should_show_new_form
        get_with_user :new
        assert_select 'form p', :count => 3
      end
      
      def test_should_add_story
        post_with_user :new, :story => {
          :name => 'test story',
          :link => 'http://www.test.com/'
        }
        assert ! assigns(:story).new_record?
        assert_redirected_to :action => 'index'
        assert_not_nil flash[:notice]
      end
      
      def test_should_reject_missing_story_attribute
        post_with_user :new, :story => { :name => 'story without a link' }
        assert assigns(:story).errors.on(:link)
      end
      ...
      def test_should_indicate_logged_in_user
        get_with_user :index
        assert_equal users(:patrick), assigns(:current_user)
        assert_select 'div#login_logout em a', '(Logout)' 
      end
      ...
      def test_should_store_user_with_story
        post_with_user :new, :story => {
          :name => 'story with user',
          :link => 'http://www.story-with-user.com/'
        }
        assert_equal users(:patrick), assigns(:story).user
      end
    
      protected
      
      def get_with_user(action, parameters = nil, session = nil, flash = nil)
        get action, parameters, :user_id => users(:patrick).id
      end
      
      def post_with_user(action, parameters = nil, session = nil, flash = nil)
        post action, parameters, :user_id => users(:patrick).id
      end
      
    end
    Thanks again!

  3. #3
    SitePoint Member
    Join Date
    Mar 2007
    Location
    Austin TX
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Same problem on same configuration

    aranwe,
    I ended up having to change some of the code in story_controller_test.rb. Have yet to get the problem resolved with account_controller_test.rb. Looks like some classes/methods have changed in the versions of Ruby/Rails that we are working with.

    Ed

  4. #4
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Houston, TX
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What did you change?

    Ed, what did you change to get the code to work?

  5. #5
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I found the problem to getting the functional tests working. The use of symbols in the protected get_with_user and post_with_user for the session information doesn't work. user_id requires single quotes around it
    Code:
    {'user_id' => users(:patrick).id}
    Also the various assigns tests should have brackets and no symbols. Here's the complete code for the story_controller_test.rb file (passes with ruby 1.8.6 and rails 1.2.3):

    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, :votes, :users
      def setup
        @controller = StoryController.new
        @request    = ActionController::TestRequest.new
        @response   = ActionController::TestResponse.new
      end
    
      # test display of index
      def test_should_show_index
        get :index
        assert_response :success
        assert_template 'index'
        assert_not_nil assigns['story']
      end
      
      #test display of new
      def test_should_show_new
        get_with_user :new
        assert_response :success
        assert_template 'new'
        assert_not_nil assigns['story']
      end
      
      #test that the form in 'new' has 3 p tags
      def test_should_show_new_form
        get_with_user :new
        assert_select 'form p', :count => 3
      end
      
      #test that stories are added OK
      def test_should_add_story
        post_with_user :new, :story => {
          :name => 'test story',
          :link => 'http://www.test.com/'
        }
        assert ! assigns['story'].new_record?
        assert_redirected_to :action => 'index'
        assert_not_nil flash[:notice]
      end
      
      # reject saving if no an attribute is missing
      def test_should_reject_missing_story_attribute
        post_with_user :new, :story => {:name => 'Story without a link'}
        assert assigns['story'].errors.on(:link)
      end
      
      def test_should_show_story
        get :show, :permalink => 'my-shiny-weblog'
        assert_response :success
        assert_template 'show'
        assert_equal stories(:one), assigns['story']
      end
      
      def test_should_show_story_vote_elements
        get :show, :permalink => 'my-shiny-weblog'
        assert_select 'h2 span#vote_score'
        assert_select 'ul#vote_history li', :count => 2
        assert_select 'div#vote_link'
      end
      
      def test_should_accept_vote
        assert stories(:two).votes.empty?
        post :vote, :id => 2
        assert ! assigns['story'].reload.votes.empty?
      end
      
      def test_should_render_rjs_after_vote_with_ajax
        xml_http_request :post, :vote, :id => 2
        assert_response :success
        assert_template 'vote'
      end
      
      def test_should_redirect_after_vote_with_get
        get :vote, :id => 2
        assert_redirected_to :action => 'show',
          :permalink => 'sitepoint-forums'
      end
      
      def test_should_show_story_submitter
        get :show, :permalink => 'my-shiny-weblog'
        assert_select 'p.submitted_by span', 'patrick'
      end
      
      def test_should_indicate_not_logged_in
        get :index
        assert_select 'div#login_logout em', 'Not logged in.'
      end
      
      def test_should_show_navigation_menu
        get :index
        assert_select 'ul#navigation li', 2
      end
      
      def test_should_indicate_logged_in_user
        get_with_user :index
        assert_equal users(:patrick), assigns['current_user']
        assert_select 'div#login_logout em a', '(Logout)'
      end
      
      def test_should_redirect_if_not_logged_in
        get :new
        assert_response :redirect
        assert_redirected_to '/account/login'
      end
      
      def test_should_store_user_with_story
        post_with_user :new, :story => {
          :name => 'story with user',
          :link => 'http://www.story-with-user.com'
        }
        assert_equal users(:patrick), assigns['story'].user
      end
      
      protected
      
      def get_with_user(action, parameters = nil, session = nil, flash = nil)
        get action, parameters, {'user_id' => users(:patrick).id}
      end
      
      def post_with_user(action, parameters = nil, session = nil, flash = nil)
        post action, parameters, {'user_id' => users(:patrick).id}
      end
      
    end
    Hope this helps someone. The issue drove me nuts today.

    Norm

  6. #6
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Houston, TX
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Norm, that was a huge help! Thanks! I thought I'd post accurate functional test files for chapter 9, as well, as I ran across similar, though many more, issues there.

    account_controller_test.rb
    Code:
    require File.dirname(__FILE__) + '/../test_helper'
    require 'account_controller'
    
    # Re-raise errors caught by the controller.
    class AccountController; def rescue_action(e) raise e end; end
    
    class AccountControllerTest < Test::Unit::TestCase
      fixtures :users, :stories, :votes
      def setup
        @controller = AccountController.new
        @request    = ActionController::TestRequest.new
        @response   = ActionController::TestResponse.new
      end
    
    def test_should_show_login_form
        get :login
        assert_response :success
        assert_template 'login'    
        assert_select 'form p', 4
      end
      
      def test_should_perform_user_login
        post :login, :login => 'patrick', :password => 'sekrit'
        assert_redirected_to :controller => 'story'
        assert_equal users(:patrick).id, session[:user_id]
        assert_equal users(:patrick), assigns['current_user']
      end
      
      def test_should_fail_user_login
        post :login, :login => 'no such', :password => 'user'
        assert_response :success
        assert_template 'login'
        assert_nil session[:user_id]
      end
      
      def test_should_redirect_after_login_with_return_url
        post :login, { :login => 'patrick', :password => 'sekrit' },
          { 'return_to' => '/story/new' }
        assert_redirected_to '/story/new'
      end
    
      def test_should_logout_and_clear_session
        post :login, :login => 'patrick', :password => 'sekrit'
        assert_not_nil assigns['current_user']
        assert_not_nil session[:user_id]
    
        get :logout
        assert_response :success
        assert_template 'logout'
        assert_select 'h2', 'Logout successful'
    
        assert_nil assigns['current_user']
        assert_nil session[:user_id]
      end
      
      def test_should_show_user
        get :show, :id => 'patrick'
        assert_response :success
        assert_template 'show'
        assert_equal users(:patrick), assigns['user']
      end
      
      def test_should_show_user
        get :show, :id => 'patrick'
        assert_response :success
        assert_template 'show'
        assert_equal users(:patrick), assigns['user']
      end
      
      def test_should_show_submitted_stories
        get :show, :id => 'patrick'
        assert_select 'div#stories_submitted div.story', :count => 2
      end
    
      def test_should_show_stories_voted_on
        get :show, :id => 'patrick'
        assert_select 'div#stories_voted_on div.story', :count => 1
      end
    end
    story_controller_test.rb
    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, :votes, :users
      
      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
      
      def test_should_show_new
        get_with_user :new
        assert_response :success
        assert_template 'new'
        assert_not_nil assigns['story']
      end
      
      def test_should_show_new_form
        get_with_user :new
        assert_select 'form p', :count => 4
      end
      
      def test_should_add_story
        post_with_user :new, :story => {
          :name => 'test story',
          :link => 'http://www.test.com/'
        }
        assert ! assigns['story'].new_record?
        assert_redirected_to :action => 'index'
        assert_not_nil flash[:notice]
      end
      
      def test_should_reject_missing_story_attribute
        post_with_user :new, :story => {
          :name => 'story without a link'
        }
        assert assigns['story'].errors.on(:link)
      end
      
      def test_should_show_story
        get :show, :permalink => 'my-shiny-weblog'
        assert_response :success
        assert_template 'show'
        assert_equal stories(:first), assigns['story']
      end
      
      def test_should_show_story_vote_elements
        get_with_user :show, :permalink => 'my-shiny-weblog'
        assert_select 'h2 span#vote_score'
        assert_select 'ul#vote_history li', :count => 2
        assert_select 'div#vote_link'
      end
      
      def test_should_accept_vote
        assert stories(:another).votes.empty?
        post_with_user :vote, :id => 2
        assert ! assigns['story'].reload.votes.empty?
      end
      
      def test_should_render_rjs_after_vote_with_ajax
        xml_http_request :post_with_user, :vote, :id => 2
        assert_response :success
        assert_template 'vote'
      end
      
      def test_should_redirect_after_vote_with_get
        get_with_user :vote, :id => 2
        assert_redirected_to :action => 'show',
          :permalink => 'sitepoint-forums'
      end
      
      def test_should_show_story_submitter
        get :show, :permalink => 'my-shiny-weblog'
        assert_select 'p.submitted_by span', 'patrick'
      end
      
      def test_should_indicate_not_logged_in
        get :index
        assert_select 'div#login_logout em', 'Not logged in.'
      end
    
      def test_should_show_navigation_menu
        get :index
        assert_select 'ul#navigation li', 3
      end
      
      def test_should_indicate_logged_in_user
        get_with_user :index
        assert_equal users(:patrick), assigns['current_user']
        assert_select 'div#login_logout em a', '(Logout)' 
      end
      
      def test_should_redirect_if_not_logged_in
        get :new
        assert_response :redirect
        assert_redirected_to '/account/login'
      end
      
      def test_should_store_user_with_story
        post_with_user :new, :story => {
          :name => 'story with user',
          :link => 'http://www.story-with-user.com/'
        }
        assert_equal users(:patrick), assigns['story'].user
      end
      
      def test_should_show_index
        get :index
        assert_response :success
        assert_template 'index'
      end
      
      def test_should_show_bin
        get :bin
        assert_response :success
        assert_template 'index'
      end
      
      def test_should_only_list_promoted_on_index
        get :index
        assert_equal [ stories(:promoted) ], assigns['stories']
      end
      
      def test_should_only_list_unpromoted_in_bin
        get :bin
        assert_equal [ stories(:another), stories(:first) ],
          assigns['stories']
      end
      
      def test_should_use_story_index_as_default
        assert_routing '', :controller => 'story', :action => 'index'
      end
      
      def test_should_show_story_on_index
        get :index
        assert_select 'h2', 'Showing 1 front page story'
        assert_select 'div#content div.story', :count => 1
      end
      
      def test_should_show_stories_in_bin
        get :bin
        assert_select 'h2', 'Showing 2 upcoming stories'
        assert_select 'div#content div.story', :count => 2
      end
      
      def test_should_store_user_with_vote
        post_with_user :vote, :id => 2
        assert_equal users(:patrick), assigns['story'].votes.last.user
      end
      
      def test_should_not_show_vote_button_if_not_logged_in
        get :show, :permalink => 'my-shiny-weblog'
        assert_select 'div#vote_link', false
      end
      
      def test_should_show_story_submitter
        get :show, :permalink => 'promoted-story'
        assert_select 'p.submitted_by a', 'john'
      end
    
      protected
      
      def get_with_user(action, parameters = nil, session = nil, flash = nil)
        get action, parameters, { 'user_id' => users(:patrick).id }
      end
      
      def post_with_user(action, parameters = nil, session = nil, flash = nil)
        post action, parameters, { 'user_id' => users(:patrick).id }
      end
    
    end

  7. #7
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Houston, TX
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Added Functionality... Tests Failing

    So I finished Patrick's book. (It was a great primer!) I added a registration page and md5 hash to passwords. The registration logs the user in and redirects to the story controller index. Everything works great. However, I still have trouble writing the tests for these things. My unit test for applying the password hash worked fine. However, my functional test on the account controller keeps breaking. I based my test off of the story controller new action. Any help in writing working, functional tests would be appreciated.

    This is the error I get:
    Code:
      1) Error:
    test_should_perform_user_registration AccountControllerTest):
    StandardError: No fixture with name 'newuser' found for table 'users'
        P:/InstantRails/ruby/lib/ruby/gem/1.8/gems/activerecord-1.15.2/lib/active_r
    ecord/fixtures.rb:491:in 'users'
        ./test/functional/account_controller_test.rb:98:in 'test_should_perform_user
    _registration'
     
    43 tests, 95 assertions, 0 failures, 1 errors
    Here's the code:

    account_controller.rb:
    Code:
    ...
    def register
     @user = User.new(params[:user])
     if request.post? and @user.save
       flash[:notice] = 'User registration succeeded.'
     
       # Login and redirect the user to the home page.
       setSession(params[:user][:login], params[:user][:password])
     end
    end
     
     
    # Protected methods
    protected
     
     
    def setSession(login, password)
     @current_user = User.find_by_login_and_password(
       login, Digest::MD5.hexdigest('--drip-drop--' + password)
     )
     unless @current_user.nil?
       session[:user_id] = @current_user.id
       unless session[:return_to].blank?
         redirect_to session[:return_to]
         session[:return_to] = nil
       else
         redirect_to :controller => 'story'
       end
     end
    end
    end
    account_controller_test.rb:
    Code:
    ...
    def test_should_perform_user_registration
     post :register, :user => {
       :login => 'newuser',
       :password => 'passw0rd',
       :name => 'New User',
       :email => 'NUser@example.com'
     }
     assert ! assigns['user'].new_record?
     assert_redirected_to :controller => 'story'
     assert_not_nil flash[:notice]
     assert_equal users(:newuser).id, session[:user_id]
     assert_equal users(:newuser), assigns['current_user']
    end
     
    ...
    Here's the story_controller_test.rb for comparison:
    Code:
    ...
    def test_should_add_story
     post_with_user :new, :story => {
       :name => 'test story',
       :link => 'http://www.test.com/'
     }
     assert ! assigns['story'].new_record?
     assert_redirected_to :action => 'index'
     assert_not_nil flash[:notice]
    end
    ...
    

  8. #8
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The error is indicating that your fixture file for users doesn't include the newuser entry. I'd check there first.

    Norm

  9. #9
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh man, thanks so much for your help guys! I was having the exact same issues.


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
  •