Frage

This is the error I got.

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>'

This is my user_friendship_test.rb code.

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     

And this is my user_friendship.rb

class UserFriendship < ActiveRecord::Base
    belongs_to :user
    belongs_to :friend, class_name: "User", foreign_key: "friend_id"
end
War es hilfreich?

Lösung

I think, problem is in following lines, you are trying to create friendship with friend: friend(:neha), you should use create friend with users(:neha) :-

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

Thanks

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top