I want to mock IServiceCollection
to check if AddSingleton
is called with a specific Interface and concrete type using Nsubstite
mocking library and xUnit
.
This is my unit test:
[Fact]
public checkIfServicesAddedTo_DI()
{
var iServiceCollectionMock = Substitute.For<IServiceCollection>();
var iConfiguration = Substitute.For<IConfiguration>();
MatchServicesManager servicesManager = new MatchServicesManager();
servicesManager.AddServices(iServiceCollectionMock, iConfiguration);
iServiceCollectionMock.Received(1).AddSingleton(typeof(IMatchManager) , typeof(MatchManager));
}
this is the implementation:
public class MatchServicesManager : IServicesManager
{
public void AddServices(IServiceCollection services, IConfiguration configuration)
{
services.AddSingleton<IMatchManager, MatchManager>();
}
}
I expect the test to succeed, but it fails with the following error:
NSubstitute.Exceptions.ReceivedCallsException : Expected to receive exactly 1 call matching: Add(ServiceDescriptor) Actually received no matching calls. Received 1 non - matching call(non - matching arguments indicated with '*' characters): Add(*ServiceDescriptor *)