我正在使用 Mockbox 编写测试并有一个我需要测试的函数,看起来像这样
public void function placeOrder() {
// do stuff
local.helper = variables.injector.getInstance("orderHelper");
local.args = { a = "hi", b = "bye" };
try {
invoke(local.helper, "print", local.args); // Call 1
}
catch (any e) {
local.args.tryOtherPrinter = true;
invoke(local.helper, "print", local.args); // Call 2
}
// do stuff
}
我正试图围绕如何测试placeOrder
和模拟print
调用,以便它第一次而不是第二次抛出异常。我认为,关键是每次传入的参数都会不同,但如果我不确定它们,它就不会正确映射到我的模拟并且不会抛出异常。
到目前为止,这是我的测试代码:
// local.order and local.helper are mocks
local.printArgs = { a = "hi", b = "bye" };
local.printArgs2 = { a = "hi", b = "bye", tryOtherPrinter = true };
// should you be able to do this and it'll throw based on the args...
local.helper.$(method = "print", throwException = true).$args(local.printArgs).$results("");
// ... and not throw based on the other args?
local.helper.$(method = "print").$args(local.printArgs2).$results("");
local.order.placeOrder();
assertEquals(2, local.helper.$count("print"), "Expected print to be called twice");
我在文档中知道你可以让它在后续调用中返回不同的结果,但在这种情况下,我需要它抛出,然后不抛出,看起来当你模拟一个方法时,它对于你是否在问是相当确定的它扔还是不扔。