gpt4 book ai didi

testing - 由于密码测试,rails 测试电子邮件验证错误?

转载 作者:行者123 更新时间:2023-11-28 20:30:56 24 4
gpt4 key购买 nike

遵循 Michael Hartl 的 rails 指南的最新版本

我的 test/models/user_test.rb 中有以下测试文件

require 'test_helper'

class UserTest < ActiveSupport::TestCase

def setup
# prevously.
#@user = User.new(name: "Example Name", email: "example@example.com")
# password and password_conformation is added after has_secure_password is implemented in User.rb
@user = User.new(name: "Example Name", email: "example@example.com", password: "foobar", password_confirmation: "foobar")
end

.... # other tests

# should be a valid user
test "should be valid" do
assert @user.valid?
end

# test for valid email addresses
test "email validation should accept valid addresses" do
valid_addresses = %w[user@example.com USER@foo.COM A_US-ER@foo.bar.org
first.last@foo.jp alice+bob@baz.cn]
valid_addresses.each do |valid_address|
@user.email = valid_address
assert @user.valid?, "#{valid_address.inspect} should be valid"
end
end

#test for invalid email addresses
test "email validation should reject invalid addresses" do
invalid_addresses = %w[user@example,com user_at_foo.org user.name@example.
foo@bar_baz.com foo@bar+baz.com]
invalid_addresses.each do |invalid_address|
@user.email = invalid_address
assert_not @user.valid?, "#{invalid_address.inspect} should be invalid"
end
end

# PROBLEM STARTS HERE !!!!
# test password minimum length as 8
test "password should have a minimum length" do
@user.password = @user.password_confirmation = "a" * 7
assert_not @user.valid?
end

..... # more other tests


end

出于某种原因,当我运行 bundle exec rake test 时它返回以下内容

  1) Failure:
UserTest#test_email_validation_should_accept_valid_addresses [/Users/jackCHH/Desktop/Websites/cloudSolar/test/models/user_test.rb:49]:
"user@example.com" should be valid


2) Failure:
UserTest#test_should_be_valid [/Users/jackCHH/Desktop/Websites/cloudSolar/test/models/user_test.rb:14]:
Failed assertion, no message given.

问题是在我添加测试脚本来测试密码长度后出现的。我还在我的模型 User.rb 中添加了 validates :password, length: { minimum: 8 }。如果我删除模型中的验证行和 password_length 测试脚本,测试将再次通过。

@user 我的电子邮件声明似乎是正确的 (example@example.com),所以我不确定为什么它现在是无效的电子邮件

我做错了什么?

最佳答案

我找到了答案。

我在@user的声明中,我的password和password_confirmation都是foobar,长度只有6,我设置的要求最少8个字符,所以报错。

修改thisfoobar的密码后,问题解决。

关于testing - 由于密码测试,rails 测试电子邮件验证错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26429074/

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