gpt4 book ai didi

ruby-on-rails - 未定义方法 `password_confirmation=' 为 nil :NilClass

转载 作者:行者123 更新时间:2023-11-28 21:30:33 25 4
gpt4 key购买 nike

在运行我的测试服时遇到这个错误,它没有显示为失败案例,而是系统上的彻底错误。

这些是我的测试:

require 'spec_helper'

describe User do

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

subject { @user }

it { should respond_to(:name) }
it { should respond_to(:username) }
it { should respond_to(:website) }
it { should respond_to(:bio) }
it { should respond_to(:profile_path) }
it { should respond_to(:cover_path) }

it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:authenticate) }
it { should respond_to(:venue_user) }
it { should respond_to(:admin) }

it { should respond_to(:twitter_token) }
it { should respond_to(:facebook_token) }
it { should respond_to(:facebook_token) }
it { should respond_to(:google_token) }

it { should be_valid }
it { should_not be_admin }

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

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

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

describe "when email is not present" do
before { @user.email = " "}
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 "email address with mixed case" do
let(:mixed_case_email) { "Foo@ExAMPle.CoM" }

it "should be saved as lower case" do
@user.email = mixed_case_email
@user.save
expect(@user.reload.email).to eq mixed_case_email.downcase
end
end

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

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

describe "with a password that's too short" do
@user.password = @user.password_confirmation = 'a' * 5
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 wq 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

describe "remember token do" do
before { @user.save }
its(:remember_token) { should_not be_blank }
end
end

到目前为止,我的模型仅包含以下内容:

class User < ActiveRecord::Base
has_secure_password
end

附加信息:

我注释掉了测试 password 和 password_confirmation 的方法,但留下了 respond_to 测试,它们通过了。查看剩余的失败测试,​​会创建一个 password_digest,因此 has_secure_password 正在运行

最佳答案

您需要将您的作业放在 before block 中。例如:

describe "when password and confirmation do not match" do
before do
@user.password_confirmation = "mismatch"
end

it { should_not be_valid }
end

您也可以将它们放在 it block 中,但设置在 before block 中看起来更好。

关于ruby-on-rails - 未定义方法 `password_confirmation=' 为 nil :NilClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24151182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com