I am trying to write test code:
public class SupplierAppServiceTests : ABCApplicationTestBase
{
private readonly ISupplierAppService _supplierAppService;
private readonly IWmsCompanyRepository _fakeRepo;
public SupplierAppServiceTests()
{
_fakeRepo = Substitute.For<IWmsCompanyRepository>();
_supplierAppService = GetRequiredService<ISupplierAppService>();
}
[Fact]
public async Task Should_Be_Generate_Code()
{
// Arrange
_fakeRepo.CreateAsync(Arg.Any<WmsCompany>())
.Returns(Task.CompletedTask);
...
}
}
But I get this error:
Autofac.Core.DependencyResolutionException : An exception was thrown while activating MyCompany.ABC.Application.CompanySuppliers.SupplierAppService.
---- Autofac.Core.DependencyResolutionException : None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'MyCompany.ABC.Application.CompanySuppliers.SupplierAppService' can be invoked with the available services and parameters:
Cannot resolve parameter 'MyCompany.ABC.Domain.Wms.Companies.IWmsCompanyRepository wmsCompanyRepository' of constructor 'Void .ctor(MyCompany.ABC.Domain.Companies.SupplierManager, MyCompany.ABC.Domain.Companies.ISupplierRepository, MyCompany.ABC.Domain.Wms.Companies.IWmsCompanyRepository)'.
WmsCompanyRepository
class (last parameter) which implements IWmsCompanyRepository
makes a request to 3rd party's webservice.
SupplierAppService
:
public class SupplierAppService : ABCAppService, ISupplierAppService
{
public SupplierAppService(SupplierManager supplierManager, ISupplierRepository supplierRepository, IWmsCompanyRepository wmsCompanyRepository)
{
this.supplierManager = supplierManager;
this.supplierRepository = supplierRepository;
this.WmsCompanyRepository = wmsCompanyRepository;
}
...
}
IWmsCompanyRepository
:
public interface IWmsCompanyRepository
{
Task CreateAsync(WmsCompany company);
}
I changed _supplierAppService
assignment in SupplierAppServiceTests
class' constructor
from:
_supplierAppService = GetRequiredService<ISupplierAppService>();
to:
_supplierAppService = new SupplierAppService(_supplierManager, _supplierRepository, _fakeRepo);
SupplierAppService
's methods use ObjectMapper
property and this time I am getting this error:
ObjectMapper -> System.ArgumentNullException : Value cannot be null. (Parameter 'provider') objectmapper
How can I inject IWmsCompanyRepository
to SupplierAppService
properly for test case?
Add your _fakeRepo
singleton to services
so that it can be resolved for SupplierAppService
.
public SupplierAppServiceTests()
{
// _fakeRepo = Substitute.For<IWmsCompanyRepository>();
_supplierAppService = GetRequiredService<ISupplierAppService>();
}
protected override void AfterAddApplication(IServiceCollection services)
{
_fakeRepo = Substitute.For<IWmsCompanyRepository>();
services.AddSingleton(_fakeRepo);
}