For some reason, when I’m debugging my unit test, I’m getting no data in the results view. I can see them in the local variables (var), but not in the results view.
I am trying to test using an SQLite in-memory database.
This is how am creating the sql lite service.
private SqliteConnection _connection;
private DbContextOptions _options;
public TestServices()
{
_connection = new SqliteConnection("datasource=:memory:");
_connection.Open();
_options = new DbContextOptionsBuilder()
.UseSqlite(_connection)
.Options;
using (var context = new ApplicationDBContext(_options))
context.Database.EnsureCreated();
}
But if I say go to inspect the context in my service call,
StockItems will be empty even though I am passing it, as you se below.
[TestCase("12121",500)]
public void Ensure_Test_Applied(string products, decimal?
expectedTotal)
{
using (var context2 = new ApplicationDBContext(_options))
{
context2.PriceLists.Add(new PriceList { Id=1,BARCODE="TEST" ,DiscountGroupId=1,Price=50.000M,IsActive=true,IsDeleted = false });
context2.DiscountGroups.Add(new DiscountGroup { Id=1,BARCODE="TEST",MinimumPurchaseQuantity=3,IsActive=true,IsDeleted=false });
context2.StockItems.Add(new StockItem { SKU="A" ,Description="Test Product 1",IsActive=true,IsDeleted=false
});
var service = new GoComparedDiscountsService(context2);
DateTime returnDate = DateTime.Now;
// ACT
var act =
service.TestDiscount(products,3,1,DiscountTypeEnum.MultiBuy);
Assert.That(expectedTotal, Is.EqualTo(act.Value));
}
}
I dont want to show my entire service call but for example what am doing is
var existingProduct = _dbContext.StockItems.Where(w => w.SKU ==
skuChar.ToString() && w.IsActive == true && w.IsDeleted ==
false).FirstOrDefault();
Where the context will be the one pass in from the unit test any ideas what could be issue I checked my flags are correct on the table.
edit
Do I merly have to just call save changes is that all am missing.