I'm using Mockbox to write tests and have a function I need to test that looks like this
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
}
I'm trying to wrap my head around how to test placeOrder
and mock the print
call so that it will throw an exception the first time and not the second time. The key, I think, is that the arguments passed in will be different each time, but if I don't nail them, it doesn't map correctly to my mock and won't throw the exception.
Here's my test code so far:
// 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");
I know in the docs that you can have it return different results on subsequent calls, but in this case I need it to throw, then not throw and it looks like when you mock a method it's fairly set in stone as to whether you are asking it to throw or not throw.