authentication - rails authenticate nomethoderror nil for nilClass -
so, i've been having issues 'authenticate' method coming in rspec nomethoderror. below code. i'm borrowing heavily michael hartl's rails tutorial learning rails , rspec.
i have added 'has_secure_password' user model told in hartl's tutorial, , have created user password_digest, method functional. tests going screwy all. wondered if installed rspec wrong, somehow. have followed tutorial tee without error getting in way this. ideas on why occurring?
my user_spec.rb:
require 'spec_helper' describe user before @user = user.new(name: "example user", email: "user@example.com") end subject { @user } { should respond_to(:name) } { should respond_to(:email) } { should respond_to(:password_digest) } { should respond_to(:password) } { should respond_to(:password_confirmation) } { should respond_to(:authenticate) } { should be_valid } describe "when name not present" before { @user.name = ' ' } { should_not be_valid} end describe "name long" before { @user.name = "a" * 51 } { should_not be_valid } end describe "when email format valid" "should valid" addresses = %w[user@foo.com a_us-er@f.b.org frst.lst@foo.jp a+b@baz.cn] addresses.each |valid_address| @user.email = valid_address @user.should be_valid end end end describe "when email format invalid" "should invalid" addresses = %w[user@foo,com user_at_foo.org example.user@foo. foo@bar_baz.com foo@bar+baz.com] addresses.each |invalid_address| @user.email = invalid_address @user.should_not be_valid end end end describe "when email taken" before user_with_same_email = @user.dup user_with_same_email.email = @user.email.upcase user_with_same_email.save end { should_not be_valid } end describe "when password not present" before { @user.password = @user.password_confirmation = " " } { should_not be_valid } end describe "when password doesn't match confirm" before { @user.password_confirmation = "mismatch" } { should_not be_valid } end describe "when password confirm nil" before { @user.password_confirmation = nil } { should be_invalid } end describe "with password that's short" before { @user.password = @user.password_confirmation = "a" * 5 } { should_not be_valid } end describe "return value of authenticate method" before { @user.save } let(:found_user) { user.find_by_email(@user.email) } describe "with valid password" { should == found_user.authenticate(@user.password) } end describe "with invalid password" let(:user_for_invalid_password) { found_user.authenticate("invalid") } { should_not == user_for_invalid_password } specify { user_for_invalid_password.should be_false } end end end
and gemfile:
source 'https://rubygems.org' gem 'rails', '3.2.13' # use activemodel has_secure_password gem 'bcrypt-ruby', '3.0.1' gem 'bootstrap-sass', '2.1' # bundle edge rails instead: # gem 'rails', :git => 'git://github.com/rails/rails.git' group :development, :test gem 'sqlite3', '1.3.5' gem 'rspec-rails', '2.11.0' gem 'guard-rspec', '1.2.1' gem 'guard-spork', '1.2.0' gem 'childprocess', '0.3.6' gem 'spork', '0.9.2' end group :development gem 'annotate', '2.5.0' end # gems used assets , not required # in production environments default. group :assets gem 'sass-rails', '3.2.5' gem 'coffee-rails', '3.2.2' gem 'uglifier', '1.2.3' end # see https://github.com/sstephenson/execjs#readme more supported runtimes # gem 'therubyracer', :platforms => :ruby gem 'jquery-rails', '2.0.2' group :test gem 'capybara', '1.1.2' gem 'rb-inotify', '0.9' gem 'libnotify', '0.5.9' end group :production gem 'pg', '0.12.2' end # use jbuilder templates json # gem 'jbuilder' # use unicorn app server # gem 'unicorn' # deploy capistrano # gem 'capistrano' # use debugger # gem 'debugger'
my user model:
class user < activerecord::base attr_accessible :email, :name, :password, :password_confirmation has_secure_password before_save { |user| user.email = email.downcase } validates :name, presence: true, length: { maximum: 50 } valid_email_regex = /\a[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :email, presence: true, uniqueness: { case_sensitive: false }, format: { with: valid_email_regex } validates :password, presence: true, length: { minimum: 6 } validates :password_confirmation, presence: true end
and below error i'm seeing rspec
1) user failure/error: { should be_valid } expected valid? return true, got false # ./spec/models/user_spec.rb:29:in `block (2 levels) in <top (required)>' 2) user return value of authenticate method invalid password failure/error: let(:user_for_invalid_password) { found_user.authenticate("invalid") } nomethoderror: undefined method `authenticate' nil:nilclass # ./spec/models/user_spec.rb:101:in `block (4 levels) in <top (required)>' # ./spec/models/user_spec.rb:104:in `block (4 levels) in <top (required)>' 3) user return value of authenticate method invalid password failure/error: let(:user_for_invalid_password) { found_user.authenticate("invalid") } nomethoderror: undefined method `authenticate' nil:nilclass # ./spec/models/user_spec.rb:101:in `block (4 levels) in <top (required)>' # ./spec/models/user_spec.rb:103:in `block (4 levels) in <top (required)>' 4) user return value of authenticate method valid password failure/error: { should == found_user.authenticate(@user.password) } nomethoderror: undefined method `authenticate' nil:nilclass # ./spec/models/user_spec.rb:97:in `block (4 levels) in <top (required)>' 5) user when email format valid should valid failure/error: @user.should be_valid expected valid? return true, got false # ./spec/models/user_spec.rb:46:in `block (4 levels) in <top (required)>' # ./spec/models/user_spec.rb:44:in `each' # ./spec/models/user_spec.rb:44:in `block (3 levels) in <top (required)>
update:
editing instance user such solved issues:
before @user = user.new(name: "example user", email: "user@example.com", password: "foobar", password_confirmation: "foobar") end
in user model you're validating presence of password in spec should pass in password new user object. should valid.
i'm guessing that's reason you're running problems authentication method well.
Comments
Post a Comment