1
I’m trying to build a unit test with . Net Core 3.1 and using Xunit. It turns out I still don’t have a database and so I use the UseInMemoryDatabase. Turns out when I make the call from PROC, I get this mistake:
Unhandled method: Fromsqlonqueryable
I would like to know how I do to test proc with inmemory bank. I could not replace the FromSqlRaw for something that works. Check out my code as it is:
public IDeadlineCardGateway CreateRepository()
{
    var logger = Mock.Of<ILogger<DeadlineCardRepository>>();
    var serviceProvider = new ServiceCollection()
        .AddEntityFrameworkInMemoryDatabase()
        .BuildServiceProvider();
    
    var builder = new DbContextOptionsBuilder<DBContext>()
       .UseInMemoryDatabase(databaseName: "LogisticaDB")
       .UseInternalServiceProvider(serviceProvider);
    _dBContext = new DBContext(builder.Options);
    return new DeadlineCardRepository(logger, null, _dBContext);
}
and here’s the test method
[Fact(DisplayName = "GetDeadlineCard: Calculation of working days")]
public async void GetDeadlineCard_ValidCEP()
{
    const string CEP_INICIAL = "23000980";
    const string CEP_FINAL = "99999999";
    const int COD_PROD = 9;
    var repository = CreateRepository();
    var expectedResult = new DeadlineCard(
        "Teste", 
        CEP_INICIAL, 
        CEP_FINAL, 
        COD_PROD
    );
    _dBContext
        .Set<DeadlineCard>()
        .FromSqlRaw($"[dbo].[SPEG8160_CALCULAR_PRAZO_ENTREGA] {CEP_INICIAL}, {COD_PROD}", 
        expectedResult);
    var result = await repository
        .GetDeadlineCard(CEP_INICIAL, COD_PROD.ToString());
        
    Assert.Equal(expectedResult.Prazo, result.Prazo);
}
The error happens on this line:
var result = await repository.GetDeadlineCard(CEP_INICIAL, COD_PROD.ToString());
Does anyone know how I fix it?
I saw that the error is happening in the class to be tested and not in the test class. Below the class:
public class DeadlineCardRepository : RepositoryBase<DeadlineCard, int>, IDeadlineCardGateway    
{
    private readonly ILogger<DeadlineCardRepository> log;
    private readonly DBContext dBContext;
    public DeadlineCardRepository(ILogger<DeadlineCardRepository> log, IMapper mapper, DBContext dBContext) : base(log, dBContext)
    {
        this.dBContext = dBContext;
        this.log = log;
    }
    public async Task<DeadlineCard> GetDeadlineCard(string cep_inicial, string codigo_produto)
    {
        //log.LogDebug("Method DBRepository GetDeadlineCard called");
                
        var prazo = await dBContext.Set<DeadlineCard>().FromSqlRaw("SPEG8160_CALCULAR_PRAZO_ENTREGA {0}, {1}", cep_inicial, codigo_produto)
            .ToListAsync();
        
        if (prazo == null)
            return null;
        return  prazo.FirstOrDefault();
    }
}
						
novic, thanks for the help. I will take your solution into consideration yes. This is my first test using . net core and in-memory.
– pnet
@pnet made a change where you can test on another provider take a look! and if it is useful accepted as a response
– novic