我遇到了一些奇怪的验证行为:它复制了我的验证错误消息,我不知道是什么原因造成的......它在 rails 控制台中没有这样做。
这是我的手机型号的验证:
# phone.rb
validates :number, :length => { :minimum => 3 }
我的规格:
require 'spec_helper'
describe Phone do
it "requires a number" do
user = User.make!
@p = Phone.new(number:nil,user_id:user.id,type:2)
@p.valid?
puts @p.errors.inspect
@p.should have(1).error_on(:number)
end
我的测试结果:
# rspec and machinist
#<ActiveModel::Errors:0x000000036f1258 @base=#<Phone id: nil, user_id: 614, kind: nil, number: nil, created_at: nil, updated_at: nil>, @messages={:number=>["is too short (minimum is 3 characters)", "is too short (minimum is 3 characters)"]}>
F
Failures:
1) Phone requires a number
Failure/Error: @p.should have(1).error_on(:number)
expected 1 error on :number, got 2
# ./spec/models/phone_spec.rb:11:in `block (2 levels) in <top (required)>'
Finished in 0.50988 seconds
1 example, 1 failure
如您所见,我两次收到“太短(最少为 3 个字符)”……这也是 /only/ 在测试期间发生的。有任何想法吗?
谢谢!