C#C
C#3y ago
Haqz

✅ xUnit | Repository not saving to database

Hello, so i started writing tests for my web api. Issue is that for whatever reason when i call the route, in test, with correct data, it doesnt seem to be saving it. It does save when I call the route normally via SwaggerUi. I tried mocking the save method in repositoey but no luck with that. Any help would be appricieted, thanks in advance.
(Will post rest of related code in next message)
CustomWebAppFactory
protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            base.ConfigureWebHost(builder);

            builder.ConfigureTestServices(services =>
            {
                var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<DataContext>));
                if (descriptor != null) services.Remove(descriptor);

                services.AddDbContext<DataContext>(o => o.UseInMemoryDatabase("InMemoryPayments"));

                var sp = services.BuildServiceProvider();
                using (var scope = sp.CreateScope())
                using (var appContext = scope.ServiceProvider.GetRequiredService<DataContext>())
                {

                    try
                    {
                        appContext.Database.EnsureCreated();
                  
                    }
                    catch (Exception ex)
                    {
                        throw;
                    }
                }

                services.AddSingleton(PaymentDataRepository.Object);
            });
        }
Was this page helpful?