Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

NoMethodError: undefined method `friend' for #<UserFriendshipTest:0x2947dd0>

The error.

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

Note: using rails 4

2 Answers

Jason Seifer - Any idea why this is happening?

Found the answer. It should be this instead of what is pasted above.

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

Also, now my assert_nothing_raised assertion is not being picked up as an assertion, know why?

I know it's an old question .. but it could help... you for got the Γ¨nd`

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