Mock Get 覆盖方法使另一个测试在 xUnit.NET 中失败
Mock Get override method make another test fail in xUnit.NET
我有两个使用 xUnit 框架的测试:
private IRepo myRepo;
public MyTests() { myRepo = Mock.Of<IRepo>(); ... }
[Fact]
public async void NoTenantTest()
{
var validJson = GetValidJson();
Mock.Get(myRepo).Setup(t => t.FindByIdAsync(It.IsAny<string>())).Returns(Task.FromResult(null as MyObject));
var request = TestFactory.CreateHttpRequest(new Dictionary<string, string> { { "tenant", "abc" } }, "https://test.com", validJson);
var ex = await Assert.ThrowsAsync<Exception>(() => myAzureFunc.Run(request, mLog, mCtx));
Assert.Equal("FindByIdAsync(abc) returned null", ex.Message, true);
}
[Fact]
public async void NoCompanyTest()
{
var validJson = GetValidJson();
Mock.Get(myRepo).Setup(t => t.FindByIdAsync(It.IsAny<string>())).Returns(Task.FromResult(new MyObject()));
...
Mock.Get(myManager).Setup(w => w.GetIdAsync(It.IsAny<MyObject>()).Returns(Task.FromResult(null as string));
var request = TestFactory.CreateHttpRequest(new Dictionary<string, string> { { "tenant", "abc" } }, "https://test.com", validJson);
var ex = await Assert.ThrowsAsync<Exception>(() => mPaymentWebhook.Run(request, mLog, mCtx));
Assert.Equal("No item found for abc", ex.Message, true);
}
xUnit 似乎按字母顺序执行测试。不知何故,来自 Mock.Get(myRepo)
的 Setup
未被覆盖,然后 NoTenantTest
失败(我收到另一条错误消息)
如何创建 IRepo 对象的新实例以避免我的情况?
如果我单独执行测试,它会工作,如果我 运行 全部,然后失败。
在单独的测试方法中创建它们(模拟),而不是作为在构造函数中初始化的共享资源。
此外,如果测试是异步的,请使用 async Task
而不是 async void
[Fact]
public async Task NoTenantTest() {
var validJson = GetValidJson();
IRepo myRepo = Mock.Of<IRepo>();
Mock.Get(myRepo)
.Setup(t => t.FindByIdAsync(It.IsAny<string>()))
.Returns(Task.FromResult(null as MyObject));
var request = TestFactory.CreateHttpRequest(new Dictionary<string, string> { { "tenant", "abc" } }, "https://test.com", validJson);
var ex = await Assert.ThrowsAsync<Exception>(() => myAzureFunc.Run(request, mLog, mCtx));
Assert.Equal("FindByIdAsync(abc) returned null", ex.Message, true);
}
[Fact]
public async Task NoCompanyTest() {
var validJson = GetValidJson();
IRepo myRepo = Mock.Of<IRepo>();
Mock.Get(myRepo)
.Setup(t => t.FindByIdAsync(It.IsAny<string>()))
.Returns(Task.FromResult(new MyObject()));
...
Mock.Get(myManager)
.Setup(w => w.GetIdAsync(It.IsAny<MyObject>())
.Returns(Task.FromResult(null as string));
var request = TestFactory.CreateHttpRequest(new Dictionary<string, string> { { "tenant", "abc" } }, "https://test.com", validJson);
var ex = await Assert.ThrowsAsync<Exception>(() => mPaymentWebhook.Run(request, mLog, mCtx));
Assert.Equal("No item found for abc", ex.Message, true);
}
我有两个使用 xUnit 框架的测试:
private IRepo myRepo;
public MyTests() { myRepo = Mock.Of<IRepo>(); ... }
[Fact]
public async void NoTenantTest()
{
var validJson = GetValidJson();
Mock.Get(myRepo).Setup(t => t.FindByIdAsync(It.IsAny<string>())).Returns(Task.FromResult(null as MyObject));
var request = TestFactory.CreateHttpRequest(new Dictionary<string, string> { { "tenant", "abc" } }, "https://test.com", validJson);
var ex = await Assert.ThrowsAsync<Exception>(() => myAzureFunc.Run(request, mLog, mCtx));
Assert.Equal("FindByIdAsync(abc) returned null", ex.Message, true);
}
[Fact]
public async void NoCompanyTest()
{
var validJson = GetValidJson();
Mock.Get(myRepo).Setup(t => t.FindByIdAsync(It.IsAny<string>())).Returns(Task.FromResult(new MyObject()));
...
Mock.Get(myManager).Setup(w => w.GetIdAsync(It.IsAny<MyObject>()).Returns(Task.FromResult(null as string));
var request = TestFactory.CreateHttpRequest(new Dictionary<string, string> { { "tenant", "abc" } }, "https://test.com", validJson);
var ex = await Assert.ThrowsAsync<Exception>(() => mPaymentWebhook.Run(request, mLog, mCtx));
Assert.Equal("No item found for abc", ex.Message, true);
}
xUnit 似乎按字母顺序执行测试。不知何故,来自 Mock.Get(myRepo)
的 Setup
未被覆盖,然后 NoTenantTest
失败(我收到另一条错误消息)
如何创建 IRepo 对象的新实例以避免我的情况?
如果我单独执行测试,它会工作,如果我 运行 全部,然后失败。
在单独的测试方法中创建它们(模拟),而不是作为在构造函数中初始化的共享资源。
此外,如果测试是异步的,请使用 async Task
而不是 async void
[Fact]
public async Task NoTenantTest() {
var validJson = GetValidJson();
IRepo myRepo = Mock.Of<IRepo>();
Mock.Get(myRepo)
.Setup(t => t.FindByIdAsync(It.IsAny<string>()))
.Returns(Task.FromResult(null as MyObject));
var request = TestFactory.CreateHttpRequest(new Dictionary<string, string> { { "tenant", "abc" } }, "https://test.com", validJson);
var ex = await Assert.ThrowsAsync<Exception>(() => myAzureFunc.Run(request, mLog, mCtx));
Assert.Equal("FindByIdAsync(abc) returned null", ex.Message, true);
}
[Fact]
public async Task NoCompanyTest() {
var validJson = GetValidJson();
IRepo myRepo = Mock.Of<IRepo>();
Mock.Get(myRepo)
.Setup(t => t.FindByIdAsync(It.IsAny<string>()))
.Returns(Task.FromResult(new MyObject()));
...
Mock.Get(myManager)
.Setup(w => w.GetIdAsync(It.IsAny<MyObject>())
.Returns(Task.FromResult(null as string));
var request = TestFactory.CreateHttpRequest(new Dictionary<string, string> { { "tenant", "abc" } }, "https://test.com", validJson);
var ex = await Assert.ThrowsAsync<Exception>(() => mPaymentWebhook.Run(request, mLog, mCtx));
Assert.Equal("No item found for abc", ex.Message, true);
}