2011-10-15 6 views
3

MoO를 사용하여 단위 테스트 용 WebOperationContext 클래스를 조롱했습니다. 하지만 CreateTextResponse (...) 메소드를 WebOperationContext 클래스에서 Message 생성을위한 조롱 된 컨텍스트에서 실행해야합니다. 그 일을하는 방법을 권해 주시겠습니까?WebOperationContext를 조롱하여 CreateTextResponse (...) 수행

편집 : 다음은 WebOperationContext에 사용중인 현재 모의 예입니다. 그러나 CreateTextResponse/CreateStreamResponse 구현할 수 없습니다.

public IAsyncResult BeginGetData(AsyncCallback asyncCallback, object asyncState) 
public Message EndGetData(IAsyncResult asyncResult) 

public class OperationContextMock : IOperationContext 
{ 
    public HttpCookieCollection Cookies { get; set; } 

    public Message CreateStreamResponse(Action<System.IO.Stream> streamWriter, string contentType) 
    { 
     throw new NotImplementedException(); 
    } 

    public Message CreateTextResponse(string text, string contentType) 
    { 
     // How to mock this method so that it returns a Message object? 
    } 

    public string LookupRequestParameter(RequestParameter requestParameter) 
    { 
     throw new NotImplementedException(); 
    } 

    public NameValueCollection QueryParameters { get; set; } 

    public NameValueCollection RequestHeaders { get; set; } 

    public Uri RequestUri { get; set; } 

    public string ResponseContentType { get; set; } 

    public string ResponseLocation { get; set; } 

    public HttpStatusCode ResponseStatusCode { get; set; } 

    public CatalogServiceOperationContextMock() 
    { 
     this.ResponseStatusCode = HttpStatusCode.OK; 
    } 
} 

답변

2

CreateTextResponse은 가상이 아니므로 moq로 모의 할 수 없습니다. 아마 CreateTextResponse 주위에 래퍼를 만들고 싶을 것이다. 단위 테스트 중에 래퍼를 조롱 할 수 있지만 런타임에 실제 WebOperationContext에 위임합니다.

+0

잘못된 방식으로 자신을 표현한 것으로 보입니다. 그렇지 않으면 응답이 제대로 표시되지 않습니다. 나는 CreateTextResponse (...) 메소드와 WebOperationContextWrapper를 가진 IWebOperationContext 인터페이스를 가지고있다. 나는 WebOperationContext 클래스의 메소드이기 때문에 조롱하는 OperationContext로부터 CreateTextResponse (...)를 실행하고 싶다 (똑같이해야한다). – kazarindn

+0

인터페이스/래퍼 클래스를 사용하는 위치에 대한 샘플 코드를 포함 시키십시오. 작동하지 않는 코드/예제를 포함하십시오. – PatrickSteele

+0

나는 똑같은 문제가있다. 구현 된 대부분의 속성이있는 WebOperationContext 주위에 래퍼가 있지만 실제 WebOperationContext와 비슷하게 동작하도록 CreateTextResponse를 구현하는 방법을 알지 못합니다. – Martin

0

IWebOperationContextWebOperationContextWrapper이있는 WCFMock을 살펴 봐야합니다. 여기에는 CreateTextResponse 방법이 포함되어 있지 않지만 시작 지점으로 사용하여 시간을 절약 할 수 있습니다. 또한 다른 인터페이스와 래퍼를 사용할 수도 있습니다.

+0

나는 이미 그와 비슷한 것을 사용하고있다. 그러나 CreateTextResponse가 WebOperationContext 클래스 (조롱 한 것이 아니라)에서 호출하고 여전히 IncomingWebRequestContext와 OutgoingWebResponseContext를 조롱하면서 테스트에서 수행해야한다. – kazarindn