![](/img/trans.png)
[英]Michael Hartl's Rails tutorial chapter 8. User Session not created
[英]Rails Tutorial by Michael Hartl Chapter 6 Authenticate User
我似乎無法弄清楚為什么這三個測試沒有通過。 我對Rails很陌生,但我覺得至少在本教程中,我似乎已經嘗試了所有方法。 我敢肯定,這很明顯,但是我尋找答案的嘗試是徒勞的。 我的失敗測試如下(摘自Michael Hartl的Rails教程)。 謝謝。
失敗:
1)如果密碼不存在,則用戶使用有效的密碼返回認證方法的值失敗/錯誤:{{eq found_user.authenticate(@ user.password)}} NoMethodError:未定義的方法對authenticate' for nil:NilClass # ./spec/models/user_spec.rb:94:in
在“(5級)
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)如果密碼不存在,則用戶使用無效的密碼返回身份驗證方法的值失敗/錯誤: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測試
需要'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
結束
用戶模型
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
您發現錯誤,因為found_user
為nil。
那是因為when password is not present
您沒有關閉when password is not present
正確描述塊。 因此,發生的情況是,使用無效密碼設置了這個無效的@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
另外,從文件底部刪除最后end
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.