使用 TypeMock 验证是否未调用具有特定参数的方法
我正在使用 Typemock 进行一些单元测试。我模拟了静态类 Widget。我想模拟 Widget.GetPrice(123) 的返回以返回值 A。
Isolate.Fake.StaticMethods<Widget>();
Isolate.WhenCalled(() => Widget.GetPrice(123)).WillReturn("A");
我还想验证 Widget.GetPrice(456) 没有被调用。
Isolate.Verify.WasNotCalled(() => Widget.GetPrice(456));
WasNotCalled 似乎没有考虑参数。测试返回说它失败了,因为实际上调用了 Widget.GetPrice。
我想到的唯一方法是执行 DoInstead 调用并在调用 Widget.GetPrice(456) 时增加计数器。测试结束时将检查计数器是否增加。有没有更好的方法来做到这一点?
I am using Typemock for some of my unit tests. I have mocked static class Widget. I want to mock the return of Widget.GetPrice(123) to return value A.
Isolate.Fake.StaticMethods<Widget>();
Isolate.WhenCalled(() => Widget.GetPrice(123)).WillReturn("A");
I also want to verify that Widget.GetPrice(456) was NOT called.
Isolate.Verify.WasNotCalled(() => Widget.GetPrice(456));
It seems that WasNotCalled does not take parameters into consideration. The test comes back saying that it failed b/c Widget.GetPrice was in fact called.
The only way I could think to do this is to do a DoInstead call and increment a counter when Widget.GetPrice(456) is called. The end of the test would check if the counter was incremented. Is there a better way to do this though?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几种方法可以做到这一点。
首先,你的 DoInstead 想法非常好,但我会调整它以包含一个断言:
这个想法是,当调用该方法时,你在调用时验证传入参数是否是你所期望的并且测试失败如果不是,则使用 Assert 语句。
(您还会注意到,我将“0”放入参数中,因为正如您所注意到的,实际值并不重要。我发现当参数不重要时,使用 null/0/etc 会更容易进行将来的维护这样您就不会“忘记”或“被愚弄”认为它确实很重要。)
您可以做的第二件事是使用 WithExactArguments 来控制行为。
“WithExactArguments”将使您的行为仅在参数匹配时运行。当您运行测试时,如果传入无效值,将引发异常并且测试将失败。
无论哪种方式,您最终都会使用“WhenCalled”部分来处理您的断言,而不是“Verify”调用。
There are a couple of ways you can do this.
First, your DoInstead idea is pretty good, but I would tweak it to contain an assertion:
The idea there is that when the method is called, you verify at the time of the call that the incoming parameter is what you expect and fail the test with the Assert statement if it's not.
(You'll also note I put "0" in as the parameter because, as you noticed, the actual value doesn't matter. I find it's easier for future maintenance to use null/0/etc when the parameters don't matter so you don't "forget" or "get fooled" into thinking that it does matter.)
The second thing you could do is use WithExactArguments to control the behavior.
The "WithExactArguments" will make your behavior run only if the arguments match. When you run your test, if an invalid value gets passed in, the exception will get thrown and the test will fail.
Either way, you end up using the "WhenCalled" part of things to handle your assertion rather than a "Verify" call.
免责声明,我在 Typemock 工作。
由于我们的 API 正在不断改进,请查看针对您的问题的另一种解决方案。您可以
用于设置您想要的参数的行为。
另外,不需要抛出任何异常。使用 DoInstead() 如下所示来验证未使用确切参数调用哪个方法。不需要内心的断言。
Disclaimer, I work in Typemock.
Since our API is constantly improving, check out another solution for your problem. You can use
for setting your behavior for arguments you want.
Also, don't need to throw any exceptions. Use DoInstead() as shown below to verify, what method wasn't called with exact arguments. No inner assertion is needed.