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

Creating the User Friendship

My very first assertion to check the flash[:error] in user_friendships controller test is blowing up

` context "#create" do context "when not logged in" do should "redirect 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(:zeb)
  end
 end
  context "with no friend_id"
   setup do
     post :create
    end

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

with this error in terminal:

1) Error:
    test: #create should set the flash error message. (UserFriendshipsControllerTest):
    NoMethodError: undefined method `empty?' for nil:NilClass

2 Answers

Jason Seifer, thanks for responding! Turns out I had two "ends" at the end of the '#new' method, thus blowing everything up.

I'm having this same problem, only I don't think I have any extra 'end's. Here's what's in my user_friendships_controller:

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]
    else
        flash[:error] = "Friend required"
        redirect_to root_path
    end
end
end