2010-03-24 23 views
10

Rhino alay çerçevesini kullanarak bir WCF istemci proxy'si ile uğraşmanın herhangi bir yolu var mı? Birim test Proxy.Close() yöntemini deniyorum ama proxy, ICommunicationObject arabirimine sahip soyut temel sınıf ClientBase<T> kullanılarak oluşturulduğundan, sınıfın iç altyapısı sahte nesne içinde olmadığından birim sınama başarısız oluyor. Kod örnekleri ile herhangi bir iyi yolu büyük takdir edilecektir.WCF İstemcisi proxy'si için en iyi yol

+0

bir makaleye bakın [barındıran-sahte olarak-Wcf hizmet] (http://bronumski.blogspot.com.au/2011/09/hosting-mock-as- wcf-service.html) ve ilgili yanıt http://stackoverflow.com/a/10306934/52277 –

cevap

20

Yapabilecekleriniz, özgün hizmet arabiriminden ve ICommunicationObject'dan gelen bir arabirim oluşturmaktır. Daha sonra bu arabirime bağlanabilir ve alay edebilir ve yine de tüm önemli yöntemlere sahip olabilirsiniz. Örneğin

:

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(); 
İlgili konular