问题描述
我是一名学生,正在学习 ruby on rails 教程。我正在使用 Sample_App 并且一切正常,直到我添加了一些密码有效性测试。现在我得到 1 个错误和 2 个失败。每次运行测试时,生成错误的测试都会发生变化,但它始终是存在验证器。失败保持不变。
ERROR["test_layout_links",#<:reporters::suite:0x000055584f4fae28>,0.00946968999960518]
test_layout_links#SiteLayoutTest (0.01s)
ArgumentError:ArgumentError:未知验证器:'PresenseValidator'
app/models/user.rb:10:in <class:User>' app/models/user.rb:1:in
'
FAIL["test_password_should_be_present_(nonblank)",#<:reporters::suite:0x00007f2b307e3c60>,0.07021111799986102] test_password_should_be_present_(nonblank)#UserTest (0.07s) 预期 true 为 nil 或 false test/models/user_test.rb:67:in `block in class:UserTest'
FAIL["test_password_should_have_minimum_length",#<:reporters::suite:0x00007f2b308e9948>,0.09405937999872549] test_password_should_have_minimum_length#UserTest (0.09s) 预期 true 为 nil 或 false test/models/user_test.rb:72:in `block in class:UserTest'
18/18:[========================================== ================================================== ================================================] 100%时间:00:00:00,时间:00:00:00
在 0.54466s 内完成 18 次测试,31 次断言,2 次失败,1 次错误,0 次跳过
class User < ApplicationRecord
before_save { email.downcase! }
validates :name,presence: true,length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email,length: { maximum: 255 },format: { with: VALID_EMAIL_REGEX },uniqueness: true
has_secure_password
validates :password,presense: true,length: { minimum: 6 }
end
是我的 user.rb 文件
require "test_helper"
class UserTest < ActiveSupport::TestCase
def setup
@user = User.new(name: "Example User",email: "user@example.com",password: "foobar",password_confirmation: "foobar")
end
test "should be valid" do
assert @user.valid?
end
test "name should be present" do
@user.name = " "
assert_not @user.valid?
end
test "email should be present" do
@user.email = " "
assert_not @user.valid?
end
test "name should not be too long" do
@user.name = "a" * 51
assert_not @user.valid?
end
test "email should not be too long" do
@user.email = "a" * 244 + "@example.com"
assert_not @user.valid?
end
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 "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 foo@bar..com]
invalid_addresses.each do |invalid_address|
@user.email = invalid_address
assert_not @user.valid?,"#{invalid_address.inspect} should be invalid"
end
end
test "email addresses should be unique" do
duplicate_user = @user.dup
@user.save
assert_not duplicate_user.valid?
end
test "email addresses should be saved as lower-case" do
mixed_case_email = "Foo@ExAMPle.CoM"
@user.email = mixed_case_email
@user.save
assert_equal mixed_case_email.downcase,@user.reload.email
end
test "password should be present (nonblank)" do
@user.password = @user.password_confirmation = " " * 5
assert_not @user.valid?
end
test "password should have minimum length" do
@user.password = @user.password_confirmation = "a" * 5
assert_not @user.valid?
end
end
是我的测试文件。这些是出现错误时我正在处理的最后两个文件。任何帮助将不胜感激,我可以提供更多信息或代码的其他部分,如果有人能弄清楚我还需要在哪里寻找正在发生的事情。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)