2013-03-22 91 views
1

我有一個驗證的模型,像這樣:如何用shoulda的消息測試mongoid模型驗證?

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 

我描述模型由rspecthoughtbot's shoulda

describe Order do 
    # validations 
    it { should validate_presence_of(:first_name) } 
    it { should presence_of(:last_name) } 
end 

,但我得到的失敗:

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)>' 

而且 '應該'在模型驗證中沒有消息的情況下工作得很好。

如何使用消息測試模型驗證?

回答

4

mongoid-rspec gem允許使用消息測試模型驗證。

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