2013-12-13 30 views
-2

當運行我的測試中,我不斷收到一個NoMethodError未定義的方法「mutual_friendship」沒有方法誤差的Rails(方法是存在的)

我想不通的原因,我在我的測試模塊調用該方法,並傳遞沒有問題,然後我添加這個新塊來調用它,它會拋出這個錯誤,但在使用該方法之前的測試仍然可以正常工作。有任何想法嗎?

此錯誤最終被上線116的整個塊是

should "delete the mutual friendship" do 
     assert_equal @friendship2, @friendship1.mutual_friendship --line 116 
     @friendship1.delete_mutual_friendship! 
     assert !UserFriendship.exists?(@friendship2.id) 
    end 

現在,這是我的UserFriendship.rb文件

def mutual_friendship 
     self.class.where({user_id: friend_id, friend_id: user_id}).first 
    end 

奇怪的是我把這種方法在下面哪個塊通過,並且在塊錯誤輸出之前。

should "accept the mutual friendship" do 
    @user_friendship.accept! 
    assert_equal 'accepted', @user_friendship.mutual_friendship.state --method called! 
end 

的@friendship被定義爲這樣:

@friendship1 = users(:user1).user_friendships.where(friend_id: users(:user2).id).first 
@friendship2 = users(:user2).user_friendships.where(friend_id: users(:user1).id).first 
+0

未定義的方法?什麼是@ @友誼1定義爲?我們能否得到完整的錯誤? – sevenseacat

回答

1

作爲@sevenseacat在暗示,@ friendship1可能不是類UserFriendship的。在行115,加

assert_not_nil @friendship1 
assert_instance_of UserFriendship, @friendship1 
+0

它確實是零!當我定義我的實例變量時,我忘了把@ friendship1 =和剛纔的友誼1 = –

1

這是不可能告訴你所提供的代碼,但我要去猜測,@friendship1實例變量實際上不是UserFriendship類的一個實例。

您可能會調用mutual_friendship而不是空的結果 - 如果users(:user1).user_friendships.where(friend_id: users(:user2).id)代碼未返回first調用的結果,則會發生這種情況。