【问题标题】:Mock Get override method make another test fail in xUnit.NET模拟获取覆盖方法使 xUnit.NET 中的另一个测试失败
【发布时间】:2020-10-15 14:41:04
【问题描述】:

我有两个使用 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 对象的新实例以避免我的情况?

如果我单独执行测试,它可以工作,如果我全部运行,则失败。

【问题讨论】:

  • xUnit 以 随机 顺序运行测试,而不是按字母顺序。它还会在每次运行时创建一个 Fresh Fixture,因此每次都会在您的 ctor 中创建 Mock 对象。你需要opt into sharing via its Fixtures mechanism得到这种行为。

标签: moq xunit.net


【解决方案1】:

在单独的测试方法中创建它们(模拟),而不是作为在构造函数中初始化的共享资源。

此外,如果测试是异步的,请使用 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);
}

【讨论】:

    猜你喜欢
    • 2020-11-05
    • 1970-01-01
    • 2021-10-07
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多