I am writing unit tests that involves a service method that takes in a Func<Task> parameter and executes it under some conditions.
The method looks like this:
public class ServiceA : IServiceA
{
public async Task TryExecute(Func<Task> func, string foo)
{
// ... do unimportant things relating to string foo
await func();
}
}
The service calling the TryExecute method looks like this
public class ServiceB : IServiceB
{
private readonly IServiceA _serviceA;
public ServiceB(IServiceA serviceA)
{
_serviceA = serviceA;
}
public async MethodThatCallsTryExecute(Bar bar)
{
// ... do unimportant things relating to bar
await _serviceA.TryExecute(privateFunction, "baz");
}
}
An example failing unit test including the attempt to set up the mocked outcome when calling TryExecute looks like this:
[Fact]
public async void TestName()
{
// Arrange
var serviceA = new Mock<IServiceA>();
serviceA.Setup(a => a.TryExecute(It.IsAny<Func<Task>>(), It.IsAny<string>()))
.Returns(async (Func<Task> func, string foo) => { await func(); });
var serviceB = new ServiceB(serviceA.Object);
var bar = new Bar();
// Act
await serviceB.MethodThatCallsTryExecute(bar);
// Assert
... failing assertions
}
Problem: during test execution, the privateFunction passed into TryExecute is not being executed (i.e. when debugging, I'm unable to step in the privateFunction method passed in).
Please let me know if there is anything more I can elaborate on; this is my first question on StackOverflow.