简体   繁体   中英

How can\should I test a conditional validation?

I am using Ruby on Rails 3.0.9 and RSpec 2. I would like to know how I can\\should run test for the following validation mathod:

class User < ActiveRecord::Base
  validates :firstname, :unless => :condition,
    :presence => true,
    ...
end

I mean the :unless => :condition part.

I am assuming you have been using shoulda matchers on Active Model to test such validations.

In that case, you can always control what instance you testing the validation on by using the "subject" block before your should "assertion" like: (for the :unless case)

context "testing validations" do
  subject do
    #Create object satisfying :condition
  end
  should_not validate_presence_of :first name
end

and the object case of the above.

通过创建满足条件或不满足条件的对象,并查看它们是否有效/无效。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM