我目前有以下测试,看起来像是一个小DRY治疗的好候选人:
describe League do
context 'attributes validation' do
before(:each) do
@league = League.new
end
it 'should be invalid without a short_name' do
@league.attributes = valid_league_attributes.except(:short_name)
@league.should_not be_valid
@league.should have(1).error_on(:short_name)
@league.errors[:short_name].should == ["can't be blank"]
@league.short_name = 'NFL'
@league.should be_valid
end
it 'should be invalid without a long_name' do
@league.attributes = valid_league_attributes.except(:long_name)
@league.should_not be_valid
@league.should have(2).error_on(:long_name)
@league.errors[:long_name].should == ["can't be blank", 'is not included in the list']
@league.long_name = 'National Football League'
@league.should be_valid
end
end
end
是否可以使用自定义匹配器或其他实用程序使其更干?
答案 0 :(得分:0)
有可能,但我不推荐它。这两个测试是完全不同的,编写一个方法来包装它们会引入比看起来更合理的复杂性,并且如果两个测试中的一个应该失败,将使故障排除更加困难。
答案 1 :(得分:0)
您可能需要查看shoulda
这将允许你写
describe League do
subject {League.new}
it {should validate_presence_of(:long_name)}
it {should validate_presence_of(:short_name)}
end
还有许多用于验证和关联的其他匹配器。