【发布时间】:2022-01-07 10:36:33
【问题描述】:
我正在使用AutoBogus 测试数据填充内存数据库,如下所示:
// Create dummy data.
var goalFaker = new AutoFaker<Goal>()
.RuleFor(g => g.Id, f => f.IndexFaker + 1); // +1 for the non-zero primary key.
var goals = goalFaker.Generate(10);
// Use a dummy in-memory database instead of a real one.
var options = new DbContextOptionsBuilder<GoalContext>()
.UseInMemoryDatabase(databaseName: "TestDatabase")
.Options;
// Save our dummy data to our dummy database.
using (var context = new GoalContext(options))
{
context.AddRange(goals);
context.SaveChanges();
}
但是,数据永远不会保存!它完全被忽略了。
我做错了什么?如何让我的测试数据在测试中持续存在?
【问题讨论】:
标签: c# entity-framework unit-testing dbcontext