使用 NSabstitute returns '0' 而不是 HttpStatusCode.OK 模拟的方法
Method mocked with NSabstitute returns '0' instead of HttpStatusCode.OK
为什么在模拟我的服务并将 return 值指定为 HttpStatusCode.OK[=31 后得到 int 值 '0' =]?
- 首先我模拟我的服务:
private readonly IService service= Substitute.For<IService>();
然后在测试中:
var statusCode = service.DoSomethingAndReturnHttpStatusCode(xmlDocument).Returns(HttpStatusCode.OK);
最后return值为statusCode = 0
我期望的是:OK
正确答案是
Returns() will only apply to given combination of arguments, so other
calls to this method will return a default value instead.
给定的参数将与配置的参数进行比较。在您的情况下,xmlDocument
的配置实例与测试下 class 中传递给方法的实例不同。
您有两个选择:
忽略给定的参数 - 但会丢失一些测试覆盖率
var result =
service.DoSomething(null).ReturnForAnyArgs(HttpStatusCode.OK);
在返回期望值之前验证给定的参数
var result = service
.DoSomething(Arg.Is<XmlDocument>(doc => doc != null))
.Returns(HttpStatusCode.OK)
为什么在模拟我的服务并将 return 值指定为 HttpStatusCode.OK[=31 后得到 int 值 '0' =]?
- 首先我模拟我的服务:
private readonly IService service= Substitute.For<IService>();
然后在测试中:
var statusCode = service.DoSomethingAndReturnHttpStatusCode(xmlDocument).Returns(HttpStatusCode.OK);
最后return值为statusCode = 0
我期望的是:OK
正确答案是
Returns() will only apply to given combination of arguments, so other calls to this method will return a default value instead.
给定的参数将与配置的参数进行比较。在您的情况下,xmlDocument
的配置实例与测试下 class 中传递给方法的实例不同。
您有两个选择:
忽略给定的参数 - 但会丢失一些测试覆盖率
var result =
service.DoSomething(null).ReturnForAnyArgs(HttpStatusCode.OK);
在返回期望值之前验证给定的参数
var result = service
.DoSomething(Arg.Is<XmlDocument>(doc => doc != null))
.Returns(HttpStatusCode.OK)