4

I'm mocking the WebOperationContext class over wrapper for unit testing (using Moq). But I need to execute CreateTextResponse(...) method from WebOperationContext class in my mocked context for Message generation. Could you please give me any recommendation how to do that?

EDIT: Below is the current mock I am using for the WebOperationContext. However, I can't get to implement 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;
    }
}
Martin
  • 39,309
  • 62
  • 192
  • 278
kazarindn
  • 169
  • 8

2 Answers2

3

CreateTextResponse is not virtual so you can't mock it with moq. You'll probably want to create a wrapper around CreateTextResponse. You can mock the wrapper during unit testing, but delegate to the actual WebOperationContext at runtime.

PatrickSteele
  • 14,489
  • 2
  • 51
  • 54
  • It looks I expressed myself in a wrong way or I'm not getting your response right. I have a IWebOperationContext interface with CreateTextResponse(...) method and a WebOperationContextWrapper. I want to execute CreateTextResponse(...) from the mocking OperationContext as it is the method from WebOperationContext class (it should do exactly the same). – kazarindn Oct 16 '11 at 07:21
  • Please include some sample code of where you're using your interfaces/wrapper classes and what you're trying to do that isn't working. – PatrickSteele Oct 16 '11 at 15:54
  • I am having the exact same issue. I have an wrapper around WebOperationContext with most of the properties implemented, but I don't see how implement CreateTextResponse so that it behaves similarly to the real WebOperationContext. – Martin May 29 '12 at 21:00
0

You should take a look at WCFMock, which has an IWebOperationContext and WebOperationContextWrapper. These don't include the CreateTextResponse method, but you can use them as a starting point to save time. You'll also likely be able to make use of the other interfaces and wrappers.

Jeff Ogata
  • 56,645
  • 19
  • 114
  • 127
  • I'm already using something like it, but I need CreateTextResponse to perform in the tests as it is invoking from the WebOperationContext class (not from mocking one) and still mock IncomingWebRequestContext and OutgoingWebResponseContext. – kazarindn Oct 16 '11 at 07:31