简体   繁体   English

Michael Hartl撰写的Rails教程第6章验证用户

[英]Rails Tutorial by Michael Hartl Chapter 6 Authenticate User

I can't for the life of me seem to figure out why these three tests aren't passing. 我似乎无法弄清楚为什么这三个测试没有通过。 I'm pretty new with rails but I feel as though I've tried about everything, at least according to the tutorial. 我对Rails很陌生,但我觉得至少在本教程中,我似乎已经尝试了所有方法。 I'm sure its something obvious but my attempts to find an answer have been in vain. 我敢肯定,这很明显,但是我寻找答案的尝试是徒劳的。 My failed tests are as follows(pulled from Michael Hartl's Rails Tutorial). 我的失败测试如下(摘自Michael Hartl的Rails教程)。 Thanks. 谢谢。

Failures: 失败:

1) User when password is not present 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:94:in block (5 levels) in ' 1)如果密码不存在,则用户使用有效的密码返回认证方法的值失败/错误:{{eq found_user.authenticate(@ user.password)}} NoMethodError:未定义的方法对authenticate' for nil:NilClass # ./spec/models/user_spec.rb:94:in在“(5级)

2) User when password is not present return value of authenticate method with invalid password Failure/Error: let(:user_for_invalid_password) { found_user.authenticate("invalid") } NoMethodError: undefined method authenticate' for nil:NilClass # ./spec/models/user_spec.rb:98:in block (5 levels) in ' # ./spec/models/user_spec.rb:100:in `block (5 levels) in ' 2)如果密码不存在,则用户使用无效的密码返回验证方法的值失败/错误:let(:user_for_invalid_password){found_user.authenticate(“ invalid”)} NoMethodError:未定义的方法authenticate' for nil:NilClass # ./spec/models/user_spec.rb:98:in块(5级)中'#./spec authenticate' for nil:NilClass # ./spec/models/user_spec.rb:98:in `在'的块(5级)中'

3) User when password is not present return value of authenticate method with invalid password Failure/Error: let(:user_for_invalid_password) { found_user.authenticate("invalid") } NoMethodError: undefined method authenticate' for nil:NilClass # ./spec/models/user_spec.rb:98:in block (5 levels) in ' # ./spec/models/user_spec.rb:101:in `block (5 levels) in ' 3)如果密码不存在,则用户使用无效的密码返回身份验证方法的值失败/错误:let(:user_for_invalid_password){found_user.authenticate(“ invalid”)} NoMethodError:未定义的方法authenticate' for nil:NilClass # ./spec/models/user_spec.rb:98:in ' authenticate' for nil:NilClass # ./spec/models/user_spec.rb:98:in块(5级)./spec authenticate' for nil:NilClass # ./spec/models/user_spec.rb:98:in `在'中的块(5级)

User_spec tests User_spec测试

require 'spec_helper' 需要'spec_helper'

describe User do

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


  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
  it { should respond_to(:password_digest) }
  it { should respond_to(:password) }
  it { should respond_to(:password_confirmation) }
  it { should respond_to(:authenticate) }

  it { should be_valid }

  describe "when name is not present" do
    before { @user.name = " " }
    it { should_not be_valid }
  end

  it "should be valid" do
    expect(@user).to be_valid
  end

  describe "when email is not present" do
    before { @user.email = " " }
    it { should_not be_valid }
  end

  describe "when name is too long" do
    before { @user.name = 'a' * 51 }
    it { should_not be_valid }
  end

  describe "when email format is invalid" do
  it "should be invalid" do
    addresses = %w[user@foo,com user_at_foo.org example.user@foo.
                   foo@bar_baz.com foo@bar+baz.com]
    addresses.each do |invalid_address|
      @user.email = invalid_address
      expect(@user).not_to be_valid
    end
  end
end

describe "when email format is valid" do
  it "should be valid" do
    addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
    addresses.each do |valid_address|
      @user.email = valid_address
      expect(@user).to be_valid
    end
  end
end

describe "when email address is already taken" do
  before do
    user_with_same_email = @user.dup
    user_with_same_email.email = @user.email.upcase
    user_with_same_email.save
  end

  it { should_not be_valid }
  end

  describe "when password is not present" do
    before do
      @user = User.new(name:"Example User", email: "user@example.com",
                        password: " ", password_confirmation: " ")
    end
    it { should_not be_valid }


  describe "when password does not match confirmation" do
    before { @user.password_confirmation = "mismatch" }
    it { should_not be_valid }
  end

  describe "with a password thats 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 end 结束

User model 用户模型

class User < ActiveRecord::Base 
  before_save { self.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, format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, length: { minimum: 6 }

end

You are getting the error as found_user is nil. 您发现错误,因为found_user为nil。

Thats because you are not closing when password is not present describe block properly. 那是因为when password is not present您没有关闭when password is not present正确描述块。 So what happens is, this invalid @user is set with a blank password and its been carried forward till your failing test case. 因此,发生的情况是,使用无效密码设置了这个无效的@user ,并将其@user到失败的测试用例。

  describe "when password is not present" do
    before do
      @user = User.new(name:"Example User", email: "user@example.com",
                       password: " ", password_confirmation: " ")
    end
    it { should_not be_valid }
  end  ### Add end here

Also, remove the very last end from the bottom of the file. 另外,从文件底部删除最后end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM