I have a .net-core web service which I use to execute argument actions with the type Expression<Action<T>>
when runing Integration tests:
// this is the usual pattern to call the service
_backgroundTasksService.RunBackgroundTask<MyService>(x => x.Execute(id, name, color));
// and this is the service which stubs the calls for unit tests:
public class BackgroundTasksServiceStub : IBackgroundTasksService {
private readonly IServiceProvider _serviceProvider;
public BackgroundTasksServiceStub(IServiceProvider serviceProvider){
_serviceProvider = serviceProvider;
}
public void RunBackgroundTask<T>(Expression<Action<T>> methodCall){
var service = _serviceProvider.GetService<T>();
var method = methodCall.Compile();
method(service);
}
}
Calling this stub service works fine when ‘MyService.Execute’ is a synchronous method, but it misbehaves when ‘MyService.Execute’ is async (async Task...
) …
What should I do so this stub service can handle the call to async methods synchronously?
Btw, I know that the actual service works fine, so there is a way! Only misbehaves on the unit tests This is the first question I got answered on this matter.