當運行我的測試中,我不斷收到一個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
未定義的方法?什麼是@ @友誼1定義爲?我們能否得到完整的錯誤? – sevenseacat