简体   繁体   English

我如何\\应该测试条件验证?

[英]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: 我正在使用Ruby on Rails 3.0.9和RSpec2。我想知道如何\\应该对以下验证方法进行测试:

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

I mean the :unless => :condition part. 我的意思是:unless => :condition部分。

I am assuming you have been using shoulda matchers on Active Model to test such validations. 我假设您一直在Active Model上使用Shoulda Matcher来测试此类验证。

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. 和上面的对象情况。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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