Ruby &引用;这里是“方法”;用红宝石?
我正在写几个助手来帮我干掉考试。我想象的是:Ruby &引用;这里是“方法”;用红宝石?,ruby,metaprogramming,heredoc,Ruby,Metaprogramming,Heredoc,我正在写几个助手来帮我干掉考试。我想象的是: 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}, #{
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\u eval
?你见过Shoulda吗?它非常适合测试诸如验证、关系等公共Rails功能。在这种情况下,似乎有必要使用class\u eval
,因为我想在实际代码中插入变量名
接受你的答案,因为我最终还是使用了Shoulda。
class MemberTest < ActiveSupport::TestCase
test_presence_validation_of :member, :name
end
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