![](/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.