xUnit 使用先前的迭代来评估使用 Theory 和 InlineData 时的不同情况
我正在尝试测试一个方法并验证是否调用了另一个方法。这取决于布尔值。如果为真,则应调用另一个方法。不过,断言偶尔会起作用,因为正确的场景可能会在错误的场景之前运行,并且 xUnit 会计算那里发生的调用。
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task Test_Resync(bool shouldUpdate)
{
...
await Resync().ConfigureAwait(false);
if (shouldUpdate)
{
A.CallTo(() => mock.AnyMethod()).MustHaveHappened();
return;
}
A.CallTo(() => mock.AnyMethod()).MustNotHaveHappened();
}
虽然我可以使用 MustHaveHappenedOnceOrLess 作为解决方法,但我认为这没有反映预期的行为,所以我的疑问是:在使用 Theory 时是否有办法拥有独立的案例?将这两种情况分开会更好吗?
I'm trying to test a method and verify whether another method was called or not. This depends on a bool value. If it's true, the other method should be called. Still, the assertion occasionally works because the true scenario might run before the false scenario, and xUnit counts the call that happens there.
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task Test_Resync(bool shouldUpdate)
{
...
await Resync().ConfigureAwait(false);
if (shouldUpdate)
{
A.CallTo(() => mock.AnyMethod()).MustHaveHappened();
return;
}
A.CallTo(() => mock.AnyMethod()).MustNotHaveHappened();
}
Although I can use MustHaveHappenedOnceOrLess as a workaround, I think this is not reflecting the behavior expected, so my doubt is: is there a way to have independent cases when using Theory? Would it be better to separate both scenarios?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
InlineData 确保独立执行,但您仍然可以使用相同的模拟。解决方案是确保模拟已重新创建。
InlineData ensures independent execution but you can still be using the same mock. The solution is to ensure the mock has been recreated.