NMock:重新定义方法期望
我是 NMock 和一般嘲笑的新手。 是否有可能重新定义期望? 事实上,我想用很多方法来模拟一个接口。 所以我决定对通用方法期望进行因式分解,这样就不必将它们写 1000 次。 我的问题如下: 我有一种加载文件的存根方法。 在大多数情况下,模拟不会对此方法执行任何操作。 所以我在 [SetUp] 中分解了期望,
Stub.On(myMock).Method("Load").Will(Return.Value(True));
但是,在测试用例中,我想测试使用模拟的对象是否对异常响应良好,所以我放入了我的测试方法:
Stub.On(myMock).Method("Load").Will(Throw.Exception(new FileNotFoundException()));
当我调试测试时,我看到 Load方法返回 True。 我可以理解这一点,但是是否可以重置方法的异常或重新定义它?
I'm new to NMock and mocking in general.
Is it possible to redefine an expectation ?
In fact, I whant to mock an interface with many methods.
So I decided to factorize common method expectations not to have to write them 1000 times.
My issue is the following :
I have a method to stub that loads a file.
In most cases, the mock will do nothing for this method.
So I factorized the expectation in [SetUp]
Stub.On(myMock).Method("Load").Will(Return.Value(True));
But, In a test case, I want to test that the object using the mock responds well to an exception, so I put in my test method :
Stub.On(myMock).Method("Load").Will(Throw.Exception(new FileNotFoundException()));
When I debug the test, I see the Load method is returning True.
I can anderstand this, but is it possible to reset exceptation for a method or redefine it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从来没有找到一个好的方法来做到这一点。我必须使用自定义操作来根据特定调用的需要设置值。像这样的事情:
我通常不会允许设置该函数,因为我通常只想偶尔返回不同的值,这可以通过更改字段来完成。确保在测试结束时重置。
是的,我知道这很糟糕,如果有人知道的话,我会喜欢更好的方法。顺便说一句 - 如果你不喜欢 NMock,你可能想看看像 Moq 这样的东西。我倾向于用它得到更好的结果,尽管显然你的里程可能会有所不同:)
I've never found a nice way to do this. I've had to use a custom action to set the value as needed for the specific call. Something like this:
I wouldn't normally allow the function to be set as I'd generally just want to return a different value ocasionally, which could be done by changing the field. Make sure to reset things at the end of the test.
Yes, I know this is pretty crappy and would love a better way if anyone knows of one. As an aside - if you aren't stuck with NMock you might want to take a look at something like Moq instead. I tend to have better results with it, although obviously your mileage may vary :)