undefined method `authenticate' for nil:NilClass

360 views
Skip to first unread message

Roelof Wobben

unread,
Jul 21, 2014, 12:19:03 PM7/21/14
to rubyonra...@googlegroups.com
Hello,

I do still follow the Hartl tutorial.
Am at chapter 6 and according to the manual the test schould be successfull but I see these error messages:

Failures:                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                    
  1) User when password doesn't match confirmation return value of authenticate method with valid password                                                                                                                                          
     Failure/Error: it { should eq found_user.authenticate(@user.password) }                                                                                                                                                                        
     NoMethodError:                                                                                                                                                                                                                                 
       undefined method `authenticate' for nil:NilClass                                                                                                                                                                                             
     # ./spec/models/user_spec.rb:91:in `block (5 levels) in <top (required)>'     


Roelof
  


Scott Ribe

unread,
Jul 21, 2014, 12:26:58 PM7/21/14
to rubyonra...@googlegroups.com
On Jul 21, 2014, at 10:19 AM, Roelof Wobben <rwo...@hotmail.com> wrote:

> undefined method `authenticate' for nil:NilClass

You're calling method authenticate nil. You need to look at your call to authenticate, and figure out why the object is nil. (This could easily be caused by a simple typo. Check variable names carefully.)


--
Scott Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice




Benjamin Iandavid Rodriguez

unread,
Jul 21, 2014, 12:36:01 PM7/21/14
to rubyonra...@googlegroups.com
It seems that you need to create the Devise mapping(If you're using devise), to fix this you can:

- Set the devise mapping inside a before block
- Include Devise test helpers

Here's a link to the Devise Wiki

Cheers,
Ian






--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/722ED808-DDC4-4D21-8AC4-A4C8FAEE329E%40elevated-dev.com.
For more options, visit https://groups.google.com/d/optout.

Eric Saupe

unread,
Jul 21, 2014, 12:43:04 PM7/21/14
to rubyonra...@googlegroups.com
@Ian,

He is not using Devise if he is going through the Hartl tutorial. Scott's suggestion is the one that should be followed.

Roelof Wobben

unread,
Jul 21, 2014, 12:53:16 PM7/21/14
to rubyonra...@googlegroups.com
Hello,

I checked and double checked it but it looks allright to me.

I even copy/paste it to my files but still no luck.

The problem must be somewhere here:

describe "with a password that's too short" do
    before { @user.password = @user.password_confirmation = "a" * 5 }
    it { should be_invalid }
  end

  describe "return value of authenticate method" do
    before { @user.save }
    let(:found_user) { User.find_by(email: @user.email) }

    describe "with valid password" do
      it { should eq found_user.authenticate(@user.password) }
    end

    describe "with invalid password" do
      let(:user_for_invalid_password) { found_user.authenticate("invalid") }

      it { should_not eq user_for_invalid_password }
      specify { expect(user_for_invalid_password).to be_false }
    end
  end
end

Roelof


Op maandag 21 juli 2014 18:43:04 UTC+2 schreef Eric Saupe:

Scott Ribe

unread,
Jul 21, 2014, 1:09:47 PM7/21/14
to rubyonra...@googlegroups.com
On Jul 21, 2014, at 10:53 AM, Roelof Wobben <rwo...@hotmail.com> wrote:

> Hello,
>
> I checked and double checked it but it looks allright to me.
>
> I even copy/paste it to my files but still no luck.
>
> The problem must be somewhere here:
>
> describe "with a password that's too short" do
> before { @user.password = @user.password_confirmation = "a" * 5 }
> it { should be_invalid }
> end
>
> describe "return value of authenticate method" do
> before { @user.save }
> let(:found_user) { User.find_by(email: @user.email) }
>
> describe "with valid password" do
> it { should eq found_user.authenticate(@user.password) }
> end
>
> describe "with invalid password" do
> let(:user_for_invalid_password) { found_user.authenticate("invalid") }
>
> it { should_not eq user_for_invalid_password }
> specify { expect(user_for_invalid_password).to be_false }
> end
> end
> end

So... You really can't figure out any reason why found_user would be nil? (Actually, there's lots here that looks suspect. But one step at a time, focus on your current problem.)

Roelof Wobben

unread,
Jul 21, 2014, 1:23:09 PM7/21/14
to rubyonra...@googlegroups.com
Nope,

Otherwise I would not ask here.

user is filled because of this :

before do
    @user = User.new(name: "Example User", email: "us...@example.com",
                     password: "foobar", password_confirmation: "foobar")

  end


Op maandag 21 juli 2014 19:09:47 UTC+2 schreef Scott Ribe:

Roelof Wobben

unread,
Jul 21, 2014, 1:48:45 PM7/21/14
to rubyonra...@googlegroups.com

Roelof


Op maandag 21 juli 2014 19:23:09 UTC+2 schreef Roelof Wobben:
Reply all
Reply to author
Forward
0 new messages