嘲笑WCF客户端代理的最佳方法 [英] Best way to mock WCF Client proxy

查看:174
本文介绍了嘲笑WCF客户端代理的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法来模拟使用犀牛制品的框架,所以我有机会获得Channel属性WCF客户端代理?我试图单元测试Proxy.Close()方法,但由于代理是使用抽象基类构建ClientBase< T> 它具有 ICommunicationObject 接口,我的单元测试失败作为类的内部基础设施不存在的模拟对象。与code样品任何好的办法将大大AP preciated。

Are there any ways to mock a WCF client proxy using Rhino mocks framework so I have access to the Channel property? I am trying to unit test Proxy.Close() method but as the proxy is constructed using the abstract base class ClientBase<T> which has the ICommunicationObject interface, my unit test is failing as the internal infrastructure of the class is absent in the mock object. Any good ways with code samples would be greatly appreciated.

推荐答案

你可以做的就是创建一个继承从原来的服务接口接口及 ICommunicationObject 。然后,您可以绑定到和模拟的接口,而且还有所有重要的方法。

What you could do is create an interface that inherits from the original service interface and ICommunicationObject. You could then bind to and mock that interface and still have all the important methods.

例如:

public interface IMyProxy : IMyService, ICommunicationObject
{
   // Note that IMyProxy doesn't implement IDisposable. This is because
   // you should almost never actually put a proxy in a using block, 
   // since there are many cases where the proxy can throw in the Dispose() 
   // method, which could swallow exceptions if Dispose() is called in the 
   // context of an exception bubbling up. 
   // This is a general "bug" in WCF that drives me crazy sometimes.
}

public class MyProxy : ClientBase<IMyService>, IMyProxy
{
   // proxy code 

}

public class MyProxyFactory
{
   public virtual IMyProxy CreateProxy()
   {
      // build a proxy, return it cast as an IMyProxy.
      // I'm ignoring all of ClientBase's constructors here
      // to demonstrate how you should return the proxy 
      // after it's created. Your code here will vary according 
      // to your software structure and needs.

      // also note that CreateProxy() is virtual. This is so that 
      // MyProxyFactory can be mocked and the mock can override 
      // CreateProxy. Alternatively, extract an IMyProxyFactory
      // interface and mock that. 
      return new MyProxy(); 
   } 
} 

public class MyClass
{
   public MyProxyFactory ProxyFactory {get;set;}
   public void CallProxy()
   {
       IMyProxy proxy = ProxyFactory.CreateProxy();
       proxy.MyServiceCall();
       proxy.Close();
   }
}


// in your tests; been a while since I used Rhino  
// (I use moq now) but IIRC...:
var mocks = new MockRepository();
var proxyMock = mocks.DynamicMock<IMyProxy>();
var factoryMock = mocks.DynamicMock<MyProxyFactory>();
Expect.Call(factoryMock.CreateProxy).Return(proxyMock.Instance);
Expect.Call(proxyMock.MyServiceCall());
mocks.ReplayAll();

var testClass = new MyClass();
testClass.ProxyFactory = factoryMock.Instance;
testClass.CallProxy();

mocks.VerifyAll();

这篇关于嘲笑WCF客户端代理的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆