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 trialYuichi Hagio
11,367 Points[HELP solved] undefiend method 'mutual_friendship' - Adding State > Destroying Friendships
I followed the video 'Destroying Friendships' (up to 3:10) I got error when trying to pass the test (context "#delete_mutual_friendship!" do) But I don't know what I am doing wrong, please help me.
1) Error:
test: #delete_mutual_friendship! should delete the mutual friendship. (UserFriendshipTest):
NoMethodError: undefined method `mutual_friendship' for nil:NilClass
test/unit/user_friendship_test.rb:117:in `block (2 levels) in <class:UserFriendshipTest>'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/shoulda-context- 1.0.2/lib/shoulda/context/context.rb:398:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/shoulda-context- 1.0.2/lib/shoulda/context/context.rb:398:in `block in create_test_from_should_hash'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/mocha- 0.10.5/lib/mocha/integration/mini_test/version_230_to_262.rb:28:in `run'
My code (user_friendship_test.rb):
context "#delete_mutual_friendship!" do
setup do
UserFriendship.request users(:john), users(:jim)
friendship1 = users(:john).user_friendships.where(friend_id: users(:jim).id).first
friendship2 = users(:jim).user_friendships.where(friend_id: users(:john).id).first
end
should "delete the mutual friendship" do
assert_equal @friendship2, @friendship1.mutual_friendship
@friendship1.delete_mutual_friendship!
assert !UserFriendship.exists?(@friendship2.id)
end
end
(user_friendship.rb)
def delete_mutual_friendship!
mutual_friendship.delete
end
My entire code for this: https://github.com/yhagio/pwitter/blob/master/test/unit/user_friendship_test.rb https://github.com/yhagio/pwitter/blob/master/app/models/user_friendship.rb
2 Answers
Jason Seifer
Treehouse Guest TeacherTry putting @ signs in front of your friendship variables:
@friendship1 =
@friendship2 =
That will give you access to the variables in the later tests.
Yuichi Hagio
11,367 PointsIt worked perfect, thank you Jason!