.callback() and promises
Closed this issue · 1 comments
At the moment, .callback()
is called and then immediately the .returns()
is called.
This means that it's not currently possible (as far as I can see) to delay the .return()
until a promise chain called in the .callback()
is resolved/rejected.
Here's an example:
groupMemberships
.setup(m => m.forEachAnimalInGroupOnDay(groupId, dayDateOnly, Moqs.It.isAny()))
.callback((g, d, fn) => fn(33).then(() => fn(44)))
.returns(() => Promise.resolve(true));
This mock can return a value before the second fn()
is called.
The solution is to check whether the lambda called in the .callback()
returns a Promise, and to block on that. However, I can see that that may have unintended consequences, such as the fn()
above is itself a mock, and therefore will need to specify whether or not it has a then
.
Ah, I now see that .returns()
also has the arguments. So I can fix the above problem with:
groupMemberships
.setup(m => m.forEachAnimalInGroupOnDay(groupId, dayDateOnly, Moqs.It.isAny()))
.returns((g, d, fn) => fn(33).then(() => fn(44)).then(()=>true));
So I'm not sure why .callback()
is needed at all.