0
Hello, I am starting a project and it is the first time I work with unit test, we are trying to perform unit test in EF Core, but when testing the Get, I am not able to get the value of _companyService, follow the test code, I had a dependency injection and I think that’s why I’m not getting it.
public class EmpresaTest
{
private readonly IEmpresaService _empresaService;
public EmpresaTest()
{
var service = new ServiceCollection();
service.AddDbContext<VetorPDVContext>((s, o) => o.UseInMemoryDatabase("pdvDI").EnableSensitiveDataLogging());
service.AddScoped<VetorPDVContext, VetorPDVContext>();
service.AddTransient<IEmpresaRepository, EmpresaRepository>();
service.AddTransient<IEmpresaService, EmpresaService>();
service.AddTransient<IEmpresaAppService, EmpresaAppService>();
var provider = service.BuildServiceProvider();
_empresaService = provider.GetService<IEmpresaService>();
InitContext();
}
public void InitContext()
{
var controller = new EmpresaAppService(_empresaService);
var empresa = new EmpresaModel { CodigoEmpresa = 1, CgcEmpresa = "00000000/0000-00", RazaoSocial = "Empresa 01", NomeFantasia = "Empresa 01" };
controller.Add(empresa);
}
[Fact]
public void TestGetAsync()
{
var result = _empresaService.Get(emp => emp.CodigoEmpresa == 1).Result.ToList();
}
}