简体   繁体   English

用RhinoMock模拟WCF客户端

[英]WCF Client Mocking with RhinoMock

I am trying to mock a WCF Client Proxy with Rhino Mock but I am not having much luck. 我正在尝试用Rhino Mock模拟WCF客户代理,但运气不佳。

    var ServiceMock = MockRepository.GeneratePartialMock<ServiceClient>();
    ServiceMock.Expect(p => p.Publish("")).IgnoreArguments().Return("Worked");

This is how I have been trying to mock the proxy out. 这就是我一直在尝试模拟代理的方式。 It is a normal set up through a constructor. 这是通过构造函数正常设置的。

This does not seem to mock the ServiceClient can anyone help? 这似乎不是在嘲笑ServiceClient有人可以帮助吗?

Should be able to do something like this: 应该能够做这样的事情:

[TestClass]
public class MyTestClass{

private IService _service;

[TestInitialize]
public void Setup(){
_service = MockRepository.GenerateStrictMock<IService, ICommunicationObject>();
}

[TestMethod]
public void TestWhatsGoingOn(){

_service.Expect(.....).Return(.....);

//This will test the close is called too (hence the ICommunicationObject above)
((ICommunicationObject)_service).Expect(r => r.Close());
}

[TestCleanup]
public void CleanItUp{
_service.VerifyAllExpectations();
}

This means you can test the close method is called too (as expected) 这意味着您可以测试close方法是否也被调用(如预期的那样)

I think you need to generate a strict mock not a partial... 我认为您需要生成严格的模拟,而不是部分...

Also of course, if you want to assert that the .Abort() call is made during exception handling and the like - you can do so with: 当然,当然,如果您想断言.Abort()调用是在异常处理等过程中进行的,则可以执行以下操作:

((ICommunicationObject)_service).Expect(r => r.Abort());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM