开发者

How can\should I test a conditional validation?

I am using Rub开发者_如何学编程y 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.


By creating objects that meet or don't meet the condition and seeing if they're valid/invalid.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜