【发布时间】:2010-03-18 12:04:31
【问题描述】:
好吧,假设我有以下模型:
class Country < ActiveRecord::Base
validates_presence_of :name
validates_presence_of :code
end
我正在为这些验证进行 rspec 单元测试。它们看起来像这样:
it "should be invalid without a name" do
country = Country.new(@valid_attributes.except(:name))
country.should_not be_valid
country.errors.on(:name).should == "can't be blank"
country.name = @valid_attributes[:name]
country.should be_valid
end
it "should be invalid without a code" do
country = Country.new(@valid_attributes.except(:code))
country.should_not be_valid
country.errors.on(:code).should == "can't be blank"
country.code = @valid_attributes[:code]
country.should be_valid
end
这看起来不太干。是否有任何 gem 或插件可以自动执行此类操作? 我想从这些方面得到一些东西:
it "should be invalid without a name" do
test_presence_validation :name
end
it "should be invalid without a code" do
test_presence_validation :code
end
【问题讨论】:
标签: ruby-on-rails tdd dry validation