为什么我的 RhinoMocks PartialMock 方法调用没有被模拟?
我正在创建一个部分模拟来测试基类的受保护辅助方法。我对受保护方法是否应该存在或者是否应该是注入依赖项的争论不太感兴趣,因为我真的很感兴趣看到下面的过程如何工作。
EnumerationServiceBase_Accessor 是 VSTS 2010 生成的私有访问对象。下面的所有内容都运行良好,除了第 17 行没有有效地设置拦截对 CreateNewContextResponse(request) 的调用的期望,该调用是在播放期间由partialTarget.EnumerateOp(request) 调用的受保护方法。相反,正在调用基类的实际实现。我在这里做错了什么?
1 PrivateObject p = new PrivateObject(mocks.PartialMock<EnumerationServiceBase>(contextManager, requestValidator, configProvider, faultProvider, logger));
2 EnumerationServiceBase_Accessor partialTarget = mocks.PartialMock<EnumerationServiceBase_Accessor>(p);
3
4 EnumerateOpRequest request = new EnumerateOpRequest()
5 {
6 Enumerate = new Enumerate()
7 {
8 Item = new EnumerateNewContext()
9 }
10 };
11
12 using (mocks.Record())
13 {
14 requestValidator.Expect(r => r.ValidateEndTo(request));
15 requestValidator.Expect(r => r.ValidateMaxElements(request, allowNulls: true));
16 partialTarget.Expect(t => t.EnumerateOp(request)).CallOriginalMethod(OriginalCallOptions.CreateExpectation);
17 partialTarget.Expect(t => t.CreateNewContextResponse(request)).Return(null);
18 contextManager.Expect(t => t.RemoveExpiredContexts());
19 }
20
21 using (mocks.Playback())
22 {
23 partialTarget.EnumerateOp(request);
24 }
这是在 EnumerationServiceBase.cs 中实现的 EnumerateOp(request)
1 public virtual EnumerateOpResponse EnumerateOp(EnumerateOpRequest request)
2 {
3 EnumerateOpResponse response = null;
4
5 if (request.Enumerate.Item is EnumerateNewContext)
6 {
7 try
8 {
9 _contextManager.RemoveExpiredContexts();
10 }
11 catch (Exception ex)
12 {
13 _logger.Warn("We're not cleaning up contexts effectively.", ex);
14 }
15
16 _requestValidator.ValidateEndTo(request);
17 _requestValidator.ValidateMaxElements(request, allowNulls: true);
18 response = CreateNewContextResponse(request);
19 }
20 else if (request.Enumerate.Item is EnumerationContextType)
21 {
22 _requestValidator.ValidateMaxElements(request, allowNulls: false);
23 response = CreateEnumerationContextResponse(request);
24 }
25 else
26 {
27 throw _faultProvider.GetItemNotRecognizedFault("The Enumerate.Item value was not of type EnumerateNewContext or EnumerationContextType.");
28 }
29 return response;
30 }
编辑:删除了不必要的信息。
I'm creating a partial mock to test a protected helper method of a base class. I'm not too interested in the debate on whether or not the protected method should be there or if it should be an injected dependency because I'm really interested in seeing the process below work.
EnumerationServiceBase_Accessor is the VSTS 2010 generated private access object. Everything below works well except for the fact that line 17 does not effectively set up an expectation that intercepts the call to CreateNewContextResponse(request), which is the protected method that is being called by partialTarget.EnumerateOp(request) during playback. Instead the actual implementation on the base class is being called. What am I doing wrong here?
1 PrivateObject p = new PrivateObject(mocks.PartialMock<EnumerationServiceBase>(contextManager, requestValidator, configProvider, faultProvider, logger));
2 EnumerationServiceBase_Accessor partialTarget = mocks.PartialMock<EnumerationServiceBase_Accessor>(p);
3
4 EnumerateOpRequest request = new EnumerateOpRequest()
5 {
6 Enumerate = new Enumerate()
7 {
8 Item = new EnumerateNewContext()
9 }
10 };
11
12 using (mocks.Record())
13 {
14 requestValidator.Expect(r => r.ValidateEndTo(request));
15 requestValidator.Expect(r => r.ValidateMaxElements(request, allowNulls: true));
16 partialTarget.Expect(t => t.EnumerateOp(request)).CallOriginalMethod(OriginalCallOptions.CreateExpectation);
17 partialTarget.Expect(t => t.CreateNewContextResponse(request)).Return(null);
18 contextManager.Expect(t => t.RemoveExpiredContexts());
19 }
20
21 using (mocks.Playback())
22 {
23 partialTarget.EnumerateOp(request);
24 }
And this is EnumerateOp(request) as implemented in the EnumerationServiceBase.cs
1 public virtual EnumerateOpResponse EnumerateOp(EnumerateOpRequest request)
2 {
3 EnumerateOpResponse response = null;
4
5 if (request.Enumerate.Item is EnumerateNewContext)
6 {
7 try
8 {
9 _contextManager.RemoveExpiredContexts();
10 }
11 catch (Exception ex)
12 {
13 _logger.Warn("We're not cleaning up contexts effectively.", ex);
14 }
15
16 _requestValidator.ValidateEndTo(request);
17 _requestValidator.ValidateMaxElements(request, allowNulls: true);
18 response = CreateNewContextResponse(request);
19 }
20 else if (request.Enumerate.Item is EnumerationContextType)
21 {
22 _requestValidator.ValidateMaxElements(request, allowNulls: false);
23 response = CreateEnumerationContextResponse(request);
24 }
25 else
26 {
27 throw _faultProvider.GetItemNotRecognizedFault("The Enumerate.Item value was not of type EnumerateNewContext or EnumerationContextType.");
28 }
29 return response;
30 }
EDIT: Removed unnecessary info.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您的
CreateNewContextResponse
受到保护并且 您不能使用Rhino Mocks 来模拟受保护的方法。The problem is that your
CreateNewContextResponse
is protected and You can't mock protected methods with Rhino Mocks.