0

这是我得到的错误。

UserFriendshipTest#test_that_creating_a_friendship_works_without_raising_an_exception:
NoMethodError: undefined method `friend' for #<UserFriendshipTest:0x2947dd0>
    test/models/user_friendship_test.rb:9:in `block (2 levels) in <class:UserFriendshipTest>'
    test/models/user_friendship_test.rb:8:in `block in <class:UserFriendshipTest>'

这是我的 user_friendship_test.rb 代码。

require 'test_helper'
class UserFriendshipTest < ActiveSupport::TestCase
    should belong_to(:user)
    should belong_to(:friend)

    test "that creating a friendship works without raising an exception" do
        assert_nothing_raised do 
            UserFriendship.create user: users(:brijesh), friend: friend(:neha)
        end
    end
end     

这是我的 user_friendship.rb

class UserFriendship < ActiveRecord::Base
    belongs_to :user
    belongs_to :friend, class_name: "User", foreign_key: "friend_id"
end
4

1 回答 1

2

我认为,问题出在以下几行,您正在尝试与 建立友谊friend: friend(:neha),您应该使用 create friend with users(:neha):-

assert_nothing_raised do 
  UserFriendship.create user: users(:brijesh), friend: users(:neha)
end

谢谢

于 2013-08-13T15:04:05.387 回答