为什么在 .Net Core 中 运行 Xunit 测试时以与常规 DbContext 相同的方式初始化时 Mock DbContext 为空
Why is Mock DbContext empty when initialized in same manner as regular DbContext when running Xunit tests in .Net Core
所以我们有一个对象来跟踪我们的测试上下文,我们正在寻找 Mock 上下文以便让它抛出异常 a la
ContextMock.Setup(x => x.SaveChangesAsync(It.IsAny())).ThrowsAsync(new DbUpdateException());
这是我们的基础上下文class
public InMemoryZZZContext(string dbName = null)
{
_dbName = dbName ?? _dbName;
var options = new DbContextOptionsBuilder<DatabaseContext>()
.UseInMemoryDatabase(databaseName: _dbName)
.ConfigureWarnings(w => w.Ignore(InMemoryEventId.TransactionIgnoredWarning))
.Options;
TestContext = new ZZZContext(options, UserClaimsServiceMock.Object);
TestContextMock = new Mock<ZZZContext>(options, UserClaimsServiceMock.Object);
}
在测试的构造函数中我们有:
public ZZZRepositoryTest()
{
Context = new InMemoryZZZContext().TestContext;
ContextMock = new InMemoryZZZContext().TestContextMock;
RepositoryMockHelper = new RepositoryMockHelper();
RepositoryUnderTest = new OrderingArgumentListTypeRepository(Context, RepositoryMockHelper.DependenciesMock.Object);
RepositoryUnderTestMock = new OrderingArgumentListTypeRepository(ContextMock.Object, RepositoryMockHelper.DependenciesMock.Object);
// We tried the following on a whim, to no effect
ContextMock.Setup(x => x.Set<OrderingArgumentListType>()).Returns(new Mock<DbSet<OrderingArgumentListType>>().Object);
}
当我们进入测试的第一行时,上下文已完全填充,但 ContextMock 中的表 none 已填充。
关于为什么 ContextMock 可能为空的任何想法?
默认情况下,模拟将覆盖任何虚拟成员,除非 CallBase
设置为 true
TestContextMock = new Mock<ZZZContext>(options, UserClaimsServiceMock.Object) {
CallBase = true
};
将 CallBase
设置为 true
,现在需要显式设置需要覆盖的成员,以便它们在执行测试时按预期运行。
ContextMock
.Setup(x => x.SaveChangesAsync(It.IsAny<MyModel>()))
.ThrowsAsync(new DbUpdateException());
因此,对于未使用上述设置覆盖的那些,上下文应按预期调用基成员,在调用 SaveChangesAsync
时应抛出异常
所以我们有一个对象来跟踪我们的测试上下文,我们正在寻找 Mock 上下文以便让它抛出异常 a la ContextMock.Setup(x => x.SaveChangesAsync(It.IsAny())).ThrowsAsync(new DbUpdateException());
这是我们的基础上下文class
public InMemoryZZZContext(string dbName = null)
{
_dbName = dbName ?? _dbName;
var options = new DbContextOptionsBuilder<DatabaseContext>()
.UseInMemoryDatabase(databaseName: _dbName)
.ConfigureWarnings(w => w.Ignore(InMemoryEventId.TransactionIgnoredWarning))
.Options;
TestContext = new ZZZContext(options, UserClaimsServiceMock.Object);
TestContextMock = new Mock<ZZZContext>(options, UserClaimsServiceMock.Object);
}
在测试的构造函数中我们有:
public ZZZRepositoryTest()
{
Context = new InMemoryZZZContext().TestContext;
ContextMock = new InMemoryZZZContext().TestContextMock;
RepositoryMockHelper = new RepositoryMockHelper();
RepositoryUnderTest = new OrderingArgumentListTypeRepository(Context, RepositoryMockHelper.DependenciesMock.Object);
RepositoryUnderTestMock = new OrderingArgumentListTypeRepository(ContextMock.Object, RepositoryMockHelper.DependenciesMock.Object);
// We tried the following on a whim, to no effect
ContextMock.Setup(x => x.Set<OrderingArgumentListType>()).Returns(new Mock<DbSet<OrderingArgumentListType>>().Object);
}
当我们进入测试的第一行时,上下文已完全填充,但 ContextMock 中的表 none 已填充。
关于为什么 ContextMock 可能为空的任何想法?
默认情况下,模拟将覆盖任何虚拟成员,除非 CallBase
设置为 true
TestContextMock = new Mock<ZZZContext>(options, UserClaimsServiceMock.Object) {
CallBase = true
};
将 CallBase
设置为 true
,现在需要显式设置需要覆盖的成员,以便它们在执行测试时按预期运行。
ContextMock
.Setup(x => x.SaveChangesAsync(It.IsAny<MyModel>()))
.ThrowsAsync(new DbUpdateException());
因此,对于未使用上述设置覆盖的那些,上下文应按预期调用基成员,在调用 SaveChangesAsync
时应抛出异常