gpt4 book ai didi

ruby-on-rails - Rspec be_valid 在有效测试中失败

转载 作者:行者123 更新时间:2023-12-05 05:26:43 29 4
gpt4 key购买 nike

我正在使用 Rails 4 和 rspec 学习 TDD。我为我的用户模型做了一些测试用例来检查密码长度。到目前为止,我有两项测试检查用户输入的密码是否太短,以及一项密码长度在 6 - 10 个字符之间。

至此,“密码太短”测试通过:

it "validation says password too short if password is less than 6 characters" do
short_password = User.create(email: "tester@gmail.com", password: "12345")
expect(short_password).not_to be_valid
end

但是,在我有有效密码的测试中,它失败了:

it "validation allows passwords larger than 6 and less than 10" do
good_password = User.create(email: "tester2@gmail.com", password: "blahblah")
expect(good_password).to be_valid
end

我得到这个错误:

Failure/Error: expect(good_password).to be_valid
expected #<User id: 1, email: "tester2@gmail.com",
created_at: "2014-06-21 02:43:42", updated_at: "2014-06-21 02:43:42",
password_digest: nil, password: nil, password_hash: "$2a$10$7u0xdDEcc6KJcAi32LBW7uzV9n7xYbfOhZWdcOnU5Cdm...",
password_salt: "$2a$10$7u0xdDEcc6KJcAi32LBW7u"> to be valid,
but got errors: Password can't be blank, Password is too short (minimum is 6 characters)
# ./spec/models/user_spec.rb:12:in `block (3 levels) in <top (required)>'

编辑:这是我的模型代码:

class User < ActiveRecord::Base

has_many :pets, dependent: :destroy
accepts_nested_attributes_for :pets, :allow_destroy => true
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: true

validates :password, presence: true, :length => 6..10, :confirmation => true

#callbacks
before_save :encrypt_password
after_save :clear_password

#method to authenticate the user and password
def self.authenticate(email, password)
user = find_by_email(email)
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
user
else
nil
end
end

#method to encrypt password
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end

#clears password
def clear_password
self.password = nil
end
end

我对为什么创建测试对象时密码为零感到困惑。

谢谢!

最佳答案

您的模型有密码存在要求,但是您有一个 after_save Hook ,它会取消密码并将记录置于无效状态。第一个测试通过了,因为您的记录总是被 after_save Hook 置于无效状态。您需要重新考虑如何处理密码存储;一旦你解决了这个问题,这里有一些代码示例可以帮助你提供一些测试方法:

# Set up a :user factory in spec/factories.rb; it should look something like:
FactoryBot.define do
factory :user do
sequence(:email) { |n| "tester+#{n}@gmail.com" }
password { SecureRandom.hex(6) }
end
end

# In your spec:
let(:user) { create :user, password: password }

context 'password' do
context 'length < 6' do
let(:password) { '12345' }


it { expect(user).not_to be_valid }
it { user.errors.message[:password]).to include('something') }
end

context 'length >= 6' do
context 'length < 10' do
let(:password) { 'blahblah' }

it { expect(user).to be_valid }
end

context 'length >= 10' do
let(:password) { 'blahblahblah' }

it { expect(user).not_to be_valid }
end
end
end

您还可以使用 shoulda matchers :

it { should_not allow_value('12345').for(:password) }
it { should allow_value('12345blah').for(:password) }

关于ruby-on-rails - Rspec be_valid 在有效测试中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24337849/

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