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 Building Social Features in Ruby on Rails Building the Friendship UI Creating the Friendship Controller

undefined method 'authenticate' for nil:NilClass


I'm unable to get past the "should get redirected to the login page" test. I've already included resources :user_friendships in my routes and have created a "new" view and action.

Here is my code:

My user_friendships_controller_test.rb:

require 'test_helper'

class UserFriendshipsControllerTest < ActionController::TestCase

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


class UserFriendship < ActiveRecord::Base
    belongs_to :user
    belongs_to :friend, class_name: "User", foreign_key: "friend_id"


        def friendship_params
            params.require(:user).permit(:user_id, :friend_id, :friend)

the exact error I'm getting:

  1) Error:
UserFriendshipsControllerTest#test_: #new when not logged in should get redirected to the login page. :
NoMethodError: undefined method `authenticate!' for nil:NilClass
    test/controllers/user_friendships_controller_test.rb:8:in `block (3 levels) in <class:UserFriendshipsControllerTest>'

1 runs, 0 assertions, 0 failures, 1 errors, 0 skips


We need to see your UserFriendshipController.