在我的代码中,我有类似于以下人为示例的代码。
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
任何有关如何测试此类代码的指针将不胜感激!