【发布时间】:2012-11-08 01:13:32
【问题描述】:
在我的代码中,我的代码类似于以下人为设计的示例。
class Excel
def self.do_tasks
with_excel do |excel|
delete_old_exports
export_images(excel)
export_documents(excel)
end
end
def with_excel
excel = WIN32OLE.connect('Excel.Application')
begin
yield excel
ensure
excel.close()
end
end
end
现在,我想为“do_tasks”方法编写一个测试,在其中设置方法调用的期望值,看看这些期望值是否得到满足。
我尝试了以下方法(使用 shoulda-context 和 test-unit)。但是,最后三个模拟的预期失败(模拟没有被调用)。
class ExcelTest < ActiveSupport::TestCase
should "call the expected methods" do
mock.proxy(Excel).with_excel
mock(Excel).delete_old_exports
mock(Excel).export_images.with_any_args
mock(Excel).export_documents.with_any_args
Excel.do_tasks
end
end
任何有关如何测试此类代码的指针将不胜感激!
【问题讨论】:
-
使用 rr 查看stackoverflow.com/a/19201353/449531 的类似问题和解决方案。
标签: ruby testing mocking double block