【发布时间】:2014-01-27 23:29:33
【问题描述】:
我正在使用自定义测试框架,我们正在尝试扩展一些断言功能,以在断言失败时包含自定义错误消息。当前的断言是这样调用的:
assert_compare(first_term, :neq, second_term) do
puts 'foobar'
end
我们想要具有以下功能的东西:
assert_compare(first_term, :neq, second_term, error_message) do
puts 'foobar'
end
这样,如果块失败,错误消息将描述失败。然而,我认为这很丑陋,因为我们要远离的框架就是这样做的,我必须经历很多看起来像这样的陈述:
assert.compare(variable_foo['ARRAY1'][2], variable_bar['ARRAY2'][2], 'This assert failed because someone did something unintelligent when writing the test. Probably me, since in am the one writing this really really long error statement on the same line so that you have to spend a quarter of your day scrolling to the side just to read it')
这种类型的方法调用难以阅读,即使使用变量表示错误消息也是如此。我觉得应该有更好的方法。
assert_compare(first_term, :neq, second_term) do
puts 'foobar'
end on_fail: 'This is a nice error message'
对我来说,这是最好的方法,但我不知道如何或是否有可能在 ruby 中完成此操作。
这里的目标是使其尽可能美观。有什么建议吗?
【问题讨论】:
-
您可以将 assert compare 包装在另一个采用 proc 和 final 参数的方法中。
-
如何传递对错误消息的某种引用,而不是消息本身(例如,文件名、常量、哈希键等)?在您的“更好的方式”中,似乎该消息将被硬连线到该方法。对吗?
-
作为 SO 新手,您可能需要查看此 FAQ。将来,请考虑将某个答案的复选标记延迟一段时间,因为这有时会阻碍其他答案的发布。
标签: ruby parameters lambda block proc