3

I need to mock the WebOperationContext and specifically the IncomingRequest with a header that assigns the Accept value, as to test that the value is being read properly and the OutgoingReponse.ContentType matches that the desired format. I'm using WCFMock and all was well with the general testing, but I can't get my head around what I need to do to mock the Incoming Response.

The Accept property is readonly so assigning is directly can't happen. I've tried adding the setter to WCFMock.IncomingWebRequestContextWrapper which predictably bombs since it's inheriting from System.SericeModel.Web.IncomingWebRequestContext.

So something like this would be desired

[Test]
public void SerializeObjectToXMLTest()
{
    var fake = new FakeRest();

        var mockContext = new Mock<IWebOperationContext> { DefaultValue = DefaultValue.Mock };

        using (new MockedWebOperationContext(mockContext.Object))
        {
            // WHAT I WOULD LOVE:
             MockedWebOperationContext.Current.IncomingRequest.Accept = "application/json";

            fake.SetResponseContentType();
        }

        // Assert
        mockContext.VerifySet(c => c.OutgoingResponse.ContentType, "application/json");

}

I'm afraid I have to totally mock a WebRequest and I'm looking to avoid that if possible.

bryan
  • 1,031
  • 2
  • 17
  • 36
  • 1
    Can you refactor your service code to only minimally depend on the `WebOperationContext`? That way, you can have part of the service that knows it's a service, and part that does all the work. Unit test this second part. In your tests, pass it the `IncomingRequest.Accept` – John Saunders Jan 19 '12 at 22:03
  • 1
    That's a good idea, thanks. Basically have a base class that the service(s) inherit from, and that base does the work. I could pull the values out there and send them to some other class that takes in the args and returns the values that the service then can use to create the outgoing WebContext. Cheers, that'll at least test what I want to test without fully recreating the WebRequest type in some mock setup. Cheers! – bryan Jan 20 '12 at 18:31
  • yea no idea why he didn't add some way to be able to set these properties – PositiveGuy Oct 29 '13 at 21:08
  • John, are you saying to have 3 projects, one that basically holds the .svc.cs (services) and then your WCF Service holds the related Service Interfaces (not implemented classes) that are decorated with the WCF related attributes? – PositiveGuy Oct 29 '13 at 21:10
  • John can you please explain in detail what you meant as in refactoring depdencies, I'd like to learn about what you are suggesting but I need specifics and to understand better what you mean – PositiveGuy Oct 31 '13 at 03:30

0 Answers0