Я следую инструкциям Майкла Хартла и у меня возникает проблема, когда я пытаюсь создать пользователя в своей таблице users
.
Когда я запускаю User.create
из консоли Rails, я всегда получаю:
@messages={:email=>["is invalid"]}"
от user.errors
. Также мой тест не пройден:
FAIL["test_email_validation_should_accept_valid_addresses", #<Minitest::Reporters::Suite:0x00007f66146c67f0 @name="UserTest">, 0.05937384600110818]
test_email_validation_should_accept_valid_addresses#UserTest (0.06s)
"lala@example.com" should be valid
Я полагаю, что это проблема, но я понятия не имею, почему.
Также это не удается:
test "should be valid" do
assert @user.valid?
end
Если я пишу вот так проходит тест:
test "should be valid" do
assert_not @user.valid?
end
Также не проходит:
test "email validation should accept valid addresses" do
valid_addresses = %w[lala@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 accept valid addresses" do
valid_addresses = %w[lala@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_not @user.valid?, "#{valid_address.inspect} should be valid"
end
end
Это Охранные ошибки:
FAIL["test_email_validation_should_accept_valid_addresses", #<Minitest::Reporters::Suite:0x00007f6614891a58 @name="UserTest">, 0.05828664900036529]
test_email_validation_should_accept_valid_addresses#UserTest (0.06s)
"lala@example.com" should be valid
test/models/user_test.rb:39:in `block (2 levels) in <class:UserTest>'
test/models/user_test.rb:37:in `each'
test/models/user_test.rb:37:in `block in <class:UserTest>'
FAIL["test_should_be_valid", #<Minitest::Reporters::Suite:0x00007f6614859ab8 @name="UserTest">, 0.06591933299932862]
test_should_be_valid#UserTest (0.07s)
Expected false to be truthy.
test/models/user_test.rb:11:in `block in <class:UserTest>'
Это мой user_test.rb:
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
@user = User.create(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[lala@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]
invalid_addresses.each do |invalid_address|
@user.email = invalid_address
assert_not @user.valid?, "#{invalid_address.inspect} should be invalid"
end
end
test "email address should be unique" do
duplicate_user = @user.dup
@user.save
assert_not duplicate_user.valid?
end
test "password should present (nonblank)" do
@user.password = @user.password_confirmation = " " * 5
assert_not @user.valid?
end
test "password should have a minimum length" do
@user.password = @user.password_confirmation = "a" * 5
assert_not @user.valid?
end
end
Это мой user.rb
:
class User < ApplicationRecord
before_save { self.email = email.downcase}
validates :name, presence: true, length: {maximum: 50}
VALID_EMAIL_REGEX = /[\w+\-.]+@[a-z\-.]+\.[a-z]+/i
validates :email, presence: true, length: { maximum: 255},
format: { with: /VALID_EMAIL_REGEX/},
uniqueness: false
has_secure_password
validates :password, presence:true, length: { minimum: 6}
end