Ruby on rails 3 rspec电子邮件验证

Ruby on rails 3 rspec电子邮件验证,ruby-on-rails-3,email-validation,Ruby On Rails 3,Email Validation,我正在学习Rspec。当我运行电子邮件验证测试用例时,我遇到了一个无法理解的错误。请解释 1) User should check for email validation Failure/Error: @test_user.should have(1).errors_on(:email) expected 1 errors on :email, got 2 # ./spec/models/user_spec.rb:17:in `block (2 level

我正在学习Rspec。当我运行电子邮件验证测试用例时,我遇到了一个无法理解的错误。请解释

  1) User should check for email validation
     Failure/Error: @test_user.should have(1).errors_on(:email)
       expected 1 errors on :email, got 2
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

Finished in 0.12903 seconds
2 examples, 1 failure

如果您同时使用状态验证器和某种格式验证器来验证email属性,那么将该属性设置为nil将导致两个错误

也许可以尝试以下方式:

validates :email, presence: true, 
                  format: { with: %r\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/,
                            allow_nil: true  }

我只是重读了你的问题,显然我读得不够透彻。我可能猜@test_用户的状态在测试之间没有重置。您能否提供设置此变量的等级库文件部分?
validates :email, presence: true, 
                  format: { with: %r\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/,
                            allow_nil: true  }