gpt4 book ai didi

c# - 使用 Moq 在 UnitTest 中获取调用方法的返回值

转载 作者:行者123 更新时间:2023-12-04 07:56:44 27 4
gpt4 key购买 nike

我正在使用 Moq 进行单元测试。在我尝试单元测试的方法中,我试图确保它不会引发异常,但我还想获取我正在单元测试的方法的返回值以验证结果。

_myService.Invoking(x => x.TestMethod(request)).Should().NotThrow();
以上工作。测试通过。但是,我想获得 TestMethod 的返回值.

最佳答案

好吧,事情就是这样,试着测试你想要得到什么,而不是你不想要什么:
不好(确保我们不会出现异常):

_myService.Invoking(x => x.TestMethod(request)).Should().NotThrow();
好(确保我们得到我们想要的):
var result = _myService.TestMethod(request);
// whatever asserts you want to perform on `result`
// e.g.
// Assert.IsNotNull(result);
// Assert.IsTrue(result.Count == 2);
// Assert.AreEqual(result[0],"Foo");
// etc.

关于c# - 使用 Moq 在 UnitTest 中获取调用方法的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66663311/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com