RhinoMocks - 存根方法仅调用一次
这是一些测试代码:
var searchCommand = MockRepository.GenerateStub<ISearchCommand>();
activityCreatedDateQuery = new ActivityCreatedDateQuery(searchCommand, maxRows);
searchCommand.Stub(x => x.GetResults(activityCreatedDateQuery))
.Return(GetCreatedDateQueryMockData()));
事情是这样的...当我最初调用 activityCreatedDateQuery.ExecuteQuery()
时,它在内部调用 searchCommand.GetResults(this)
、GetCreatedDateQueryMockData()
按预期调用。
我第二次在线程中调用此函数时,不会调用 GetCreatedDateQueryMockData()
,而是调用前一次调用的结果(导致 IDataReader 关闭异常)。
这显然是设计的行为,所以我如何确保总是调用委托...我已经探索了 Stub.Repeat.WhenCalled.CallBack 等,但没有运气...
Here's some test code:
var searchCommand = MockRepository.GenerateStub<ISearchCommand>();
activityCreatedDateQuery = new ActivityCreatedDateQuery(searchCommand, maxRows);
searchCommand.Stub(x => x.GetResults(activityCreatedDateQuery))
.Return(GetCreatedDateQueryMockData()));
Here's the thing...when I initially call activityCreatedDateQuery.ExecuteQuery()
which internally calls searchCommand.GetResults(this)
, GetCreatedDateQueryMockData()
is called as expected.
The second time I call this in the thread, GetCreatedDateQueryMockData()
is not called, instead the previous call's result is (resulting in an IDataReader closed exception).
This is obviously behaviour by design, so how do I make sure the delegate is always called...i've explored Stub.Repeat.WhenCalled.CallBack etc with no luck...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是“Return”不接受委托,它只接受一个对象。 GetCreatedDataQueryMockData() 方法调用在设置 Stub 时执行,并且该值作为返回值保存在框架内。
您需要的是每次调用存根时都会调用一个真正的委托。最近有人在 Stackoverflow 上问过这个问题,我 创建了一个小扩展方法来执行此操作。
The problem is that "Return" doesn't take a delegate, it just takes an object. The GetCreatedDataQueryMockData() method call is executed at the time your Stub is set and the value is saved inside the framework as the return value.
What you need is a true delegate to be called every time the stub is called. Someone else asked about this on Stackoverflow recently and I created a little extension method to do this.
我有类似的问题。
在我的案例中起作用。它之所以有效,是因为“WhenCalled()”与“Return()”不同,它使用委托。
使用“WhenCalled()”时,不要忘记在“Return()”中放置一个虚拟值。
I had a similar problem.
worked in my case. It works because "WhenCalled()" unlike "Return()" uses delegates.
When using "WhenCalled()" do not forget to put a dummy value in "Return()".
尝试
Try