Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rspec 如何使用shoulda提供的消息测试mongoid模型验证?_Rspec_Mongoid_Shoulda - Fatal编程技术网

Rspec 如何使用shoulda提供的消息测试mongoid模型验证?

Rspec 如何使用shoulda提供的消息测试mongoid模型验证?,rspec,mongoid,shoulda,Rspec,Mongoid,Shoulda,我有一个带有验证的模型,如下所示: class Order include Mongoid::Document field :first_name, type: String field :last_name, type: String validates_presence_of :first_name, :message => "Can't be empty" validates_presence_of :last_name, :message => "Ca

我有一个带有验证的模型,如下所示:

class Order
  include Mongoid::Document

  field :first_name, type: String
  field :last_name, type: String

  validates_presence_of :first_name, :message => "Can't be empty"
  validates_presence_of :last_name, :message => "Can't be empty"
end
我通过
rspec
描述模型,并:

但我会失败:

Failures:

  1) Order 
     Failure/Error: it { should validate_presence_of(:first_name) }
       Expected errors to include "can't be blank" when first_name is set to nil, got errors: ["first_name Can't be empty (nil)", "last_name Can't be empty (nil)"]
     # ./spec/models/order_spec.rb:10:in `block (2 levels) in <top (required)>'
故障:
1) 命令
失败/错误:它{应该验证是否存在(:first_name)}
当first_name设置为nil时,预期的错误包括“不能为空”,得到的错误:[“first_name不能为空(nil)”,“last_name不能为空(nil)”]
#./spec/models/order_spec.rb:10:in'block(2层)in'
此外,“应该”——在模型验证中没有消息的情况下,案例工作得非常好

如何使用消息测试模型验证?

gem允许使用消息测试模型验证

it { should validate_uniqueness_of(:email).with_message("is already taken") }
it { should validate_uniqueness_of(:email).with_message("is already taken") }