Ruby中的“这里方法”?

| 我正在写一些帮助程序来干燥我的测试。我的照片是这样的:
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
?     
已邀请:
你见过Shoulda吗?这对于测试常见的Rails功能(例如验证,关系等)非常有用。https://github.com/thoughtbot/shoulda-matchers     
在这种情况下,似乎I 4是必需的,因为我想将变量名插值到实际代码中。 图示在这里。     

要回复问题请先登录注册