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?
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 theOutgoingWebResponseContext
to return aWebHeaderCollection
. For my test case I simply assert on this collection.