Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Building Social Features in Ruby on Rails Building the Friendship UI Creating the Friendship

Lisa Rossiter
Lisa Rossiter
3,630 Points

Error with testing friend, and user_firendship

Error

admins-imac:ujunto admin$ ruby -I test test/controllers/user_friendships_controller_test.rb
Run options: --seed 51952

# Running tests:

..........FF..

Finished tests in 0.277235s, 50.4987 tests/s, 75.7480 assertions/s.

  1) Failure:
UserFriendshipsControllerTest#test_: when logged in with a valid friend_id should assign a friend object.  [test/controllers/user_friendships_controller_test.rb:92]:
--- expected
+++ actual
@@ -1 +1 @@
-#<User id: 830138774, first_name: "sam", last_name: "toms", profile_name: "sup", email: "modernoozedesign2@gmail.com", encrypted_password: "password1", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2014-04-26 13:08:15", updated_at: "2014-04-26 13:08:15">
+nil


  2) Failure:
UserFriendshipsControllerTest#test_: when logged in with a valid friend_id should assign a user_friendship object.  [test/controllers/user_friendships_controller_test.rb:98]:
--- expected
+++ actual
@@ -1 +1 @@
-#<User id: 902541635, first_name: "Bob", last_name: "Barker", profile_name: "bobb", email: "bob.barker@example.com", encrypted_password: "password1", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2014-04-26 13:08:15", updated_at: "2014-04-26 13:08:15">
+nil


14 tests, 21 assertions, 2 failures, 0 errors, 0 skips
admins-imac:ujunto admin$ 

Tests

context "with a valid friend_id" do
      setup do
        post :create, friend_id: users(:john).id
      end

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

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

class UserFriendshipsController < ApplicationController
  before_filter :authenticate_user!, only: [:new]

  def new
    if params[:friend_id]
      @friend = User.where(profile_name: params[:friend_id]).first
      raise ActiveRecord::RecordNotFound if @friend.nil?
      @user_friendship = current_user.user_friendships.new(friend: @friend)
    else
      flash[:error] = "Friend required"
    end
    rescue ActiveRecord::RecordNotFound
      render file: 'public/404', status: :not_found
  end

  def create
    if params[:friend_id]
      @friend = User.where(profile_name: params[:friend_id]).first
      @user_friendship = current_user.user_friendships.new(friend: @friend)
    else
        flash[:error] = "Friend required"
        redirect_to root_path
    end
  end
end

Fixtures

sam:
  first_name: "sam"
  last_name: "toms"
  profile_name: "tomzie"
  email: "modernoozedesign@gmail.com"
john:
  first_name: "sam"
  last_name: "toms"
  profile_name: "sup"
  email: "modernoozedesign2@gmail.com"
  encrypted_password: "password1"
bob:
  first_name: "Bob"
  last_name: "Barker"
  profile_name: "bobb"
  email: "bob.barker@example.com"
  encrypted_password: "password1"

1 Answer

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Lisa Rossiter in your test try changing the following:

    post :create, friend_id: users(:john).id

To:

    post :create, friend_id: users(:john).profile_name

That's what it looks for in the controllers so it should fix it.