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

NameError: undefined local variable or method `do_put'

I get error in my user_friendship controller_test.rb "nameerror do_put" and " Expected exactly 0 elements matching "div#friend-list", found 1.. Expected: 0,Actual: 1". I don't know why it's give name error, i check many time but i found nothing. I'm still searching what exactly i'm missing in my code.

          $ rake test
          DL is deprecated, please use Fiddle
           Run options: --seed 59616

   # Running:

.......................................................................................................E......E......E..FE.FF........F.F.......... ......

Finished in 30.629097s, 4.9626 runs/s, 7.8030 assertions/s.

   1) Error:
      UserFriendshipsControllerTest#test_: #accept when logged in should assign a user_friendship. :
    NameError: undefined local variable or method `do_put' for #<UserFriendshipsControllerTest:0x9111168>
test/controllers/user_friendships_controller_test.rb:277:in `block (3 levels) in <class:UserFriendshipsControllerTest>'


    2) Error:
     UserFriendshipsControllerTest#test_: #accept when logged in should have a flash success message. :
    NameError: undefined local variable or method `do_put' for #<UserFriendshipsControllerTest:0x68ca7b0>
test/controllers/user_friendships_controller_test.rb:288:in `block (3 levels) in <class:UserFriendshipsControllerTest>'


        3) Error:
        UserFriendshipsControllerTest#test_: #accept when logged in should create activity. :
     NameError: undefined local variable or method `do_put' for #<UserFriendshipsControllerTest:0x8a7ec00>
     test/controllers/user_friendships_controller_test.rb:294:in `block (4 levels) in <class:UserFriendshipsControllerTest>'
      test/controllers/user_friendships_controller_test.rb:293:in `block (3 levels) in <class:UserFriendshipsControllerTest>'


       4) Failure:
     UserFriendshipsControllerTest#test_: #index when logged in pending friendships should not display pending or active friend's names.  [C:/Users/Arv

ind/project/book/test/controllers/user_friendships_controller_test.rb:78]: Expected exactly 0 elements matching "div#friend-list", found 1.. Expected: 0 Actual: 1

         5) Error:
     UserFriendshipsControllerTest#test_: #accept when logged in should update tha state to accepted. :
     NameError: undefined local variable or method `do_put' for #<UserFriendshipsControllerTest:0x8ffafc0>
test/controllers/user_friendships_controller_test.rb:283:in `block (3 levels) in <class:UserFriendshipsControllerTest>'


          6) Failure:
        UserFriendshipsControllerTest#test_: #index when logged in accepted friendships should not display pending or active friend's names.  [C:/Users/Ar

           9) Failure:
                   UserFriendshipsControllerTest#test_: #index when  logged in requested friendships should not display pending or active friend's names.  [C:/Users/A

rvind/project/book/test/controllers/user_friendships_controller_test.rb:97]: Expected exactly 0 elements matching "div#friend-list", found 1.. Expected: 0 Actual: 1

            152 runs, 239 assertions, 5 failures, 4 errors, 0 skips

This is my user_friendship_controller_test.rb

 require 'test_helper'

  class UserFriendshipsControllerTest <      ActionController::TestCase
context "#index" do
  context "when not logged in" do
    should "redirect to the login page" do
      get :index
      assert_response :redirect
    end
  end

  context "when logged in" do
    setup do
      @pending_friendship = create(:pending_user_friendship, user: users(:sarah), friend: create(:user, first_name: 'Pending', last_name: 'Friend'))
      @accepted_friendship = create(:accepted_user_friendship, user: users(:sarah), friend: create(:user, first_name: 'Active', last_name: 'Friend'))
      @requested_friendship = create(:requested_user_friendship, user: users(:sarah), friend: create(:user, first_name: 'Requested', last_name: 'Friend'))
      @blocked_friendship = user_friendships(:blocked_by_sarah)

      sign_in users(:sarah)
      get :index
    end

    should "get the index page without error" do
      assert_response :success
    end

    should "assign user_friendships" do
      assert assigns(:user_friendships)
    end

    should "display friend's names" do
      assert_match(/Pending/, response.body)
      assert_match(/Active/, response.body)
    end

    should "display pending information on a pending friendship" do
      assert_select "#user_friendship_#{@pending_friendship.id}" do
        assert_select "em", "Friendship is pending."
      end
    end

    should "display date information on an accepted friendship" do
      assert_select "#user_friendship_#{@accepted_friendship.id}" do
        assert_select "em", "Friendship is accepted."
      end
    end

    context "blocked users" do
      setup do
        get :index, list: 'blocked'
      end

      should "get the index without error" do
        assert_response :success
      end

      should "not display pending or active friend's names" do
        assert_no_match(/Pending\ Friends/, response.body)
        assert_no_match(/Active\ Friends/, response.body)
      end

      should "display blocked friend names" do
        assert_match(/Blocked\ Friend/, response.body)
      end
    end

     context "pending friendships" do
      setup do
        get :index, list: 'pending'
      end

      should "get the index without error" do
        assert_response :success
      end

      should "not display pending or active friend's names" do
       assert_select "div#friend-list", {count: 1, html: /#{@pending_friendship.friend.full_name}/ }
       assert_select "div#friend-list", {count: 0, html: /#{@blocked_friendship.friend.full_name}/ }
      end

      should "display blocked friends" do
        assert_match(/Pending/, response.body)
      end
    end

     context "requested friendships" do
      setup do
        get :index, list: 'requested'
      end

      should "get the index without error" do
        assert_response :success
      end

      should "not display pending or active friend's names" do
        assert_select "div#friend-list", {count: 1, html: /#{@requested_friendship.friend.full_name}/ }
        assert_select "div#friend-list", {count: 0, html: /#{@blocked_friendship.friend.full_name}/ }
        assert_select "div#friend-list", {count: 0, html: /#{@pending_friendship.friend.full_name}/ }
        assert_select "div#friend-list", {count: 0, html: /#{@accepted_friendship.friend.full_name}/ }
      end

      should "display requested friends" do
        assert_match(/Requested/, response.body)
      end
    end

     context "accepted friendships" do
      setup do
        get :index, list: 'accepted'
      end

      should "get the index without error" do
        assert_response :success
      end

      should "not display pending or active friend's names" do
        assert_select "div#friend-list", {count: 1, html: /#{@accepted_friendship.friend.full_name}/ }
        assert_select "div#friend-list", {count: 0, html: /#{@blocked_friendship.friend.full_name}/ }
      end

      should "display requested friends" do
        assert_match(/Active/, response.body)
      end
    end
  end
end

context "#new" do
  context "when not logged in" do
    should "redirect to the login page" do
      get :new
      assert_response :redirect
    end
  end

  context "when logged in" do
    setup do
      sign_in users(:sarah)
    end

    should "get new and return success" do
      get :new
      assert_response :success
    end

    should "should set a flash error if the friend_id params is missing" do
      get :new, {}
      assert_equal "Friend required", flash[:error]
     end

     should "display the friend's name" do
       get :new, friend_id: users(:ruby)
       assert_match(/#{users(:ruby).full_name}/, response.body)
       end

       should "assign a new user friendship" do
        get :new, friend_id: users(:ruby)
        assert assigns(:user_friendship) 
        end

       should "assign a new user friendship to the correct friend" do
        get :new, friend_id: users(:ruby)
        assert_equal users(:ruby), assigns(:user_friendship).friend
        end

       should "assign a new user friendship to the currently logged in user" do
        get :new, friend_id: users(:ruby)
        assert_equal users(:sarah), assigns(:user_friendship).user
        end

        should "returns a 404 status if no friend is found" do
          get :new, friend_id: 'invalid'
          assert_response :not_found
        end

        should "ask if you really want to friend the user" do
          get :new, friend_id: users(:ruby)
          assert_match(/Do you really want to friend #{users(:ruby).full_name}?/, response.body)
        end
  end
end

context "#create" do
  context "when not logged in" do
    should "redirected to the login page" do
      get :new
      assert_response :redirect
      assert_redirected_to login_path
    end
  end

  context "when logged in" do
    setup do
      sign_in users(:sarah)
    end

    context "with no friend_id" do
      setup do
        post :create
      end

      should "set the flash error message" do
        assert !flash[:error].empty?
      end

      should "redirect to the site root" do
        assert_redirected_to root_path
      end
    end

    context "successfully" do
      should "create two user friendship objects" do
        assert_difference 'UserFriendship.count', 2 do
          post :create, user_friendship: {friend_id: users(:arvind).profile_name}
        end
      end
    end

    context "with a valid friend_id" do
        setup do
          post :create, user_friendship: {friend_id: users(:arvind).profile_name}
  end

  should "assign a friend object" do
    assert assigns(:friend)
    assert_equal users(:arvind), assigns(:friend)
      end

      should "assign a user_friendship object" do
        assert assigns(:user_friendship)  
        assert_equal users(:sarah), assigns(:user_friendship).user
        assert_equal users(:arvind), assigns(:user_friendship).friend
       end

       should "create a friendship" do
         assert users(:sarah).pending_friends.include?(users(:arvind))
       end

       should "redirect to the profile page of the friend" do
         assert_response :redirect
         assert_redirected_to profile_path(users(:arvind))
       end

       should "set the flash success message" do
         assert flash[:success]
         assert_equal "Friend request sent.", flash[:success]
       end
     end
    end
end


context "#accept" do
  context "when not logged in" do
    should "redirect to the login page" do
      put :accept, id: 1
      assert_response :redirect
      assert_redirected_to login_path
    end
   end

    context "when logged in" do
      setup do
        @friend = create(:user)
        @user_friendship = create(:pending_user_friendship, user: users(:sarah), friend: @friend)
        create(:pending_user_friendship, friend: users(:sarah), user: @friend)
        sign_in users(:sarah)
      end

     def do_put
            put :accept, id: @user_friendship
            @user_friendship.reload
     end


    should "assign a user_friendship" do
             do_put
             assert assigns(:user_friendship)
             assert_equal @user_friendship, assigns(:user_friendship)
      end

      should "update tha state to accepted" do
               do_put
               assert_equal 'accepted', @user_friendship.state
        end

       should "have a flash success message" do
            do_put
            assert_equal "You are now friends with #{@user_friendship.friend.first_name}", flash[:success]
        end

      should "create activity" do
        assert_difference "Activity.count" do
               do_put
        end
      end
    end
  end

context "#edit" do
  context "when not logged in" do
    should "redirected to the login page" do
      get :edit, id: 1
      assert_response :redirect
    end
  end

  context "when logged in" do
    setup do
      @user_friendship = create(:pending_user_friendship, user: users(:sarah))
      sign_in users(:sarah)
       get :edit, id: @user_friendship.friend.profile_name
    end

    should "get a edit and return success" do
     assert_response :success
    end

    should "assign a user_friendship" do
      assert assigns(:user_friendship)
    end

    should "assign a friend" do
      assert assigns(:friend)
    end
  end
end

  context "#destroy" do
     context "when not logged in" do
      should "redirect to the login page" do
       delete :destroy, id: 1
       assert_response :redirect
        assert_redirected_to login_path
    end
  end

    context "when logged in" do
      setup do
        @friend = create(:user)
        @user_friendship = create(:accepted_user_friendship, friend: @friend, user: users(:sarah))
        create(:accepted_user_friendship, friend: users(:sarah), user: @friend)

        sign_in users(:sarah)
     end

     should "delete user friendships" do
       assert_difference 'UserFriendship.count', -2 do
       delete :destroy, id: @user_friendship
       end
    end

    should "set the flash" do
      delete :destroy, id: @user_friendship
      assert_equal "Friendship destroyed", flash[:success]
     end
   end
 end

   context "#block" do
      context "when not logged in" do
        should "redirect to the login page" do
        put :block, id: 1
       assert_response :redirect
        assert_redirected_to login_path
      end
   end

      context "when logged in" do
         setup do
        @user_friendship = create(:pending_user_friendship, user: users(:sarah))
        sign_in users(:sarah)
       put :block, id: @user_friendship
        @user_friendship.reload
    end

      should "assign a user friendship" do
       assert assigns(:user_friendship)
        assert_equal @user_friendship, assigns(:user_friendship)
     end

                should "update the user friendship state to blocked" do
               assert_equal 'blocked', @user_friendship.state
            end
           end
        end
  end

2 Answers

Naomi Freeman
STAFF
Naomi Freeman
Treehouse Guest Teacher

Try and post your git repository :)

You can also Google how to "find all" in your Sublime Text. Find all and look for do_put and see what comes up. Sounds like a typo is in your code somewhere.

Naomi Freeman
STAFF
Naomi Freeman
Treehouse Guest Teacher

Try and post your git repository :)

You can also Google how to "find all" in your Sublime Text. Find all and look for do_put and see what comes up. Sounds like a typo is in your code somewhere.

My git repository is at: https://github.com/sarahgupta022/book.git

And why i am receiving this error in my user_friendships_controller_test.rb :

    Expected exactly 0 elements matching "div#friend-list", found 1.. Expected: 0 Actual: 1