【问题标题】:Best way to mock WCF Client proxy模拟 WCF 客户端代理的最佳方法
【发布时间】:2010-03-24 16:11:09
【问题描述】:

是否有任何方法可以使用 Rhino 模拟框架模拟 WCF 客户端代理,以便我可以访问 Channel 属性?我正在尝试对 Proxy.Close() 方法进行单元测试,但由于代理是使用具有 ICommunicationObject 接口的抽象基类 ClientBase<T> 构造的,因此我的单元测试失败,因为该类的内部基础结构在模拟对象。任何带有代码示例的好方法都将不胜感激。

【问题讨论】:

标签: .net wcf web-services mocking rhino-mocks


【解决方案1】:

您可以做的是创建一个从原始服务接口和ICommunicationObject 继承的接口。然后,您可以绑定并模拟该接口,并且仍然拥有所有重要的方法。

例如:

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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多