I am having a problem using the in-memory database. I have created a new context with options to use an in-memory database but when I run my test dbProduct that is taken from the repo returns me a product that is on my applicaton's database instead of the in-memory database.
public class ProductServiceTests
{
private IRepository repo;
private ApplicationDbContext applicationDbContext;
[SetUp]
public void Setup()
{
var contextOptions = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase("SportAreteMemoryDb")
.Options;
applicationDbContext = new ApplicationDbContext(contextOptions);
applicationDbContext.Database.EnsureDeleted();
applicationDbContext.Database.EnsureCreated();
}
[Test]
public async Task Test_AddProductAsync()
{
var repo = new Repository(applicationDbContext);
IProductService productService = new ProductService(repo);
await productService.AddProductAsync(new AddProductViewModel()
{
Model = "added product model",
Description = "added product description",
Size = "added product size",
Colour = "added product colour",
Price = 100M,
CategoryId = 1,
BrandId = 1,
ImageData = "added product image link"
});
await repo.SaveChangesAsync();
var dbProduct = await repo.GetByIdAsync<Product>(1);
Assert.That(dbProduct.Description, Is.EqualTo("added product description"));
}
[TearDown]
public void TearDown()
{
applicationDbContext.Dispose();
}
}
I have tried making it from scratch again but didn't help.
I found where the problem was. I am applying some configurations in OnModelCreating of my ApplicationDbContext that's why the first element is not what I expected to be.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.