Callback cannot return a value and, therefore, should not be used to execute asynchronous code (or synchronous code to which a value must be returned). Callback is a kind of โinjection pointโ that you can connect to check the parameters passed to the method, but not change what it returns.
If you want a lambda mock, you can just use Returns :
_mockController.Setup(tc => tc.Interrupt(It.IsAny<Func<Task>>())) .Returns(async f => await f());
(I assume Interrupt returns a Task ).
test execution continues until the task is completed (despite waiting in the callback).
Yes, since Callback cannot return a value, it is always typed as Action / Action<...> , so your async lambda ends with the async void method with all the problems that arise (as described in my MSDN article).
Update:
Interruption () is actually a void method: what it does is a queue in a function (argument) until IController can stop what it does. He then calls a function - which returns the task - and expects this task
You can make fun of this behavior if it works:
List<Func<Task>> queue = new List<Func<Task>>(); _mockController.Setup(tc => tc.Interrupt(It.IsAny<Func<Task>>())) .Callback<Func<Task>>(f => queue.Add(f)); ...
source share