Question

I'm using the WCFMock to mock the WebOperationContext in my web service. The only usage is to add a custom HTTP header to the WebOperationContext.Current.OutgoingResponse.Headers collection. I'm unable to verify this using Moq. What I've already tried:

  • Verify if the Add method is getting invoked. This fails because Add is not virtual
  • Try to access the header directly from MockedWebOperationContext.Current. This is always zero in number

How can I verify in my unit test case that a custom header has been added?

Was it helpful?

Solution

Figured it out. Here's the solution for posterity.

When we create the "moq mock" for the IWebOperationContext, the example suggests that we set the property DefaultValue = DefaultValue.Mock. This will mock all dependincies including the HttpHeaders collection. I skipped this and mocked the OutgoingWebResponseContext to return a WebHeaderCollection. For my test case I simply assert on this collection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top