SitePoint Sponsor

User Tag List

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

    Rails Chapter 8 Unit Testing

    Hi
    I have been trying to running my unit tests with out errors but keep coming up with the same error:

    1) Error:
    test_user_association(VoteTest):
    NoMethodError: undefined method `user' for #<Vote:0x21e4c20>
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/base.rb:1863:in `method_missing'
    ./test/unit/vote_test.rb:11:in `test_user_association'

    I can not seem to get this to run right, I have even copied the files provided by the book to make sure it was not a typo. I still come up with the same thing. Any ideas?

    I am very new to this and think I might be missing understanding where the problem lies.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,457
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    My vote_test.rb file from chaper 8 (2nd ed.) doesn't have a test_user_association, it has a test_story_association

    Code Ruby:
    require 'test_helper'
     
    class VoteTest < ActiveSupport::TestCase
      def test_story_association
        assert_equal stories(:one), votes(:one).story
      end
     
      def test_should_be_associated_with_user
        assert_equal users(:john), votes(:two).user
      end
     
    end
    Does your votes.yml file look like

    Code Ruby:
    # Read about fixtures at [url]http://ar.rubyonrails.org/classes/Fixtures.html[/url]
     
    one:
      story: one
      user: patrick
     
    two:
      story: one 
      user: john

  3. #3
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi
    I am using an older version of the book, but I don't think that is the issue. This is what I have

    vote_test.rb
    require File.dirname(__FILE__) + '/../test_helper'

    class VoteTest < Test::Unit::TestCase
    fixtures :votes, :stories, :users

    def test_story_association
    assert_equal storiesfirst), votesfirst).story
    end

    def test_user_association
    assert_equal usersjohn), votessecond).user
    end
    end

    and my votes.yml appears to be correct:

    first:
    id: 1
    story_id: 1
    created_at: <%= Time.new.to_sdb) %>
    user_id: 1
    second:
    id: 2
    story_id: 1
    created_at: <%= Time.now.to_sdb) %>
    user_id: 2

    this is my users.yml
    patrick:
    id: 1
    login: patrick
    password: sekrit
    name: Patrick Lenz
    email: #email removed for post
    john:
    id: 2
    login: john
    password: gh752px
    name: John Doe
    email: #email removed for post

    any thoughts would be appreciated .. thanks a lot in advance

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,457
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    If you try
    Code Ruby:
    def test_user_association
       assert_equal users(:john), votes(:second).user_id
    end
    does it pass?

  5. #5
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by looloobs View Post
    NoMethodError: undefined method `user' for #<Vote:0x21e4c20>
    I think with that error I would start by checking the Vote model rather than the test. It may well be that the test is working exactly as it should and is highlighting a problem with the model definition.

    First thing to check is that the associations methods is correct in the Vote model. If you expect to have vote.user rather than vote.users, then the statement in the model needs to be:
    Code:
    belongs_to :user
    And not
    Code:
    belongs_to :users
    If you can't spot a problem in your models, it might be worth posting both your Vote and User models.

    By the way - when entering stuff in this forum, if you highlight any code and then click on the code button at the top of the edit window (#) it will surround the test with [code] tags. That will stop your code being parsed as normal text, and things like : followed by ) being turned into .

  6. #6
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah yes the problem did in fact lie in the Vote model. I knew there was something I was missing. I am so new at this I don't quite understand all the working components, but it is coming together.

    Thanks for your help and thanks for the tip on the proper way to place code in here


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
  •