Ruby on rails Rails密码验证在编写无关测试时错误地失败
我正在做MichaelHartl的Rails教程,我在 基本上,我正在构建一个Twitter克隆,以学习Rails。 在本章中,用户可以相互跟踪 编写第一个测试以检查这些关系后: 用户测试.rb 出现以下错误: 错误 触发错误的model/user.rb中的方法 其他文件Ruby on rails Rails密码验证在编写无关测试时错误地失败,ruby-on-rails,validation,testing,railstutorial.org,Ruby On Rails,Validation,Testing,Railstutorial.org,我正在做MichaelHartl的Rails教程,我在 基本上,我正在构建一个Twitter克隆,以学习Rails。 在本章中,用户可以相互跟踪 编写第一个测试以检查这些关系后: 用户测试.rb 出现以下错误: 错误 触发错误的model/user.rb中的方法 其他文件 可以找到所有其他文件我完成了相同的教程,并在我的用户模型中验证了密码,如下所示: validates :password, presence: true, length: { minimum: 6 }, allow_nil
可以找到所有其他文件我完成了相同的教程,并在我的用户模型中验证了密码,如下所示:
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
也许加上allow_nil:对
ERROR["test_should_follow_and_unfollow_a_user", UserTest, .192215816000044]
test_should_follow_and_unfollow_a_user#UserTest (6.19s)
ActiveRecord::RecordInvalid:
ActiveRecord::RecordInvalid: Validation failed:
Password can't be blank,
Password is too short (minimum is 6 characters)
app/models/user.rb:102:in `follow'
test/models/user_test.rb:100:in `block in <class:UserTest>'
class User < ApplicationRecord
has_many :microposts, dependent: :destroy
has_many :active_relationships, class_name: "Relationship",
foreign_key: "follower_id",
dependent: :destroy
has_many :following, through: :active_relationships,
source: :followed
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest
VALID_EMAIL_REGEX = /\A[\d\+\.a-z_-]+@[a-z]+\.[a-z.]+\z/i
validates(:name, presence: true, length: { maximum: 50 } )
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
has_secure_password
# follows a user
def follow(other_user)
following << other_user # LINE OF THE ERROR (rb:102)
end
michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest('password') %>
admin: true
activated: true
activated_at: <%= Time.zone.now %>
archer:
name: Sterling Archer
email: duchess@example.gov
password_digest: <%= User.digest('password') %>
activated: true
activated_at: <%= Time.zone.now %>
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true