2010-03-24 4 views
10

Rhino mock 프레임 워크를 사용하여 WCF 클라이언트 프록시를 조롱하여 채널 속성에 액세스 할 수있는 방법이 있습니까? 내가 단위 테스트 Proxy.Close() 메서드를 시도하고 있지만 ICommunicationObject 인터페이스를 가진 ClientBase<T> 추상 기본 클래스를 사용하여 프록시를 구성 할 때 클래스의 내부 인프라가 모의 개체에 부재로 내 단위 테스트가 실패합니다. 코드 샘플을 사용한 좋은 방법은 크게 감사 할 것입니다.WCF 클라이언트 프록시를 모방하는 가장 좋은 방법

+0

이 문서를 참조 [호스팅 - 모의로서의 WCF 서비스 (http://bronumski.blogspot.com.au/2011/09/hosting-mock-as- wcf-service.html) 및 관련 답변 http://stackoverflow.com/a/10306934/52277 –

답변

20

원래 서비스 인터페이스와 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(); 
관련 문제