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


[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

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


    def delete_mutual_friendship!

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
Jason Seifer
Treehouse Guest Teacher

Try putting @ signs in front of your friendship variables:

@friendship1 =
@friendship2 =

That will give you access to the variables in the later tests.

It worked perfect, thank you Jason!