テストを DRY するためのヘルパーをいくつか書いています。私は次のようなものを描いた:
class ActiveSupport::TestCase
def self.test_presence_validation_of model, attribute
test "should not save #{model.to_s} with null #{attribute.to_s}", <<-"EOM"
#{model.to_s} = Factory.build #{model.to_sym}, #{attribute.to_sym} => nil
assert !#{model.to_s}.save, '#{model.to_s.capitalize} with null #{attribute.to_s} saved to the Database'
EOM
# Another one for blank attribute.
end
end
したがって、これは次のとおりです。
class MemberTest < ActiveSupport::TestCase
test_presence_validation_of :member, :name
end
MemberTest
クラススコープでこれを正確に実行します:
test 'should not save member with null name' do
member = Factory.build :member, :name => nil
assert !member.save, 'Member with null name saved to the Database'
end
このようにすることは可能ですか(もちろん、いくつかの適応があります。私の「写真」が機能するとは思えません)、または使用する必要がありますclass_eval
か?