How do you mock IContextChannel in a WCF ServiceAuthorizationManager?

c#, moq, unit-testing, wcf

Solution

You can't directly. See if WCFMock will help.

Problem

I am trying to write unit tests for a custom ServiceAuthorizationManager. The call to CheckAccessCore takes an OperationContext as a parameter. To instantiate an OperationContext, one must pass an IContextChannel to the constructor. Using MOQ, I've declared an IContextChannel: ``` private OperationContext _context; private Mock<IContextChannel> _contextChannelMock; ``` Then I attempt to create the OperationContext: ``` _context = new OperationContext(_contextChannelMock.Object); ``` But this line throws an exception: Result Message: Initialization method Urs.EnterpriseServices.Providers.Tests.UrsServiceAuthorizationManager_Tests.SetUp threw exception. System.InvalidOperationException: System.InvalidOperationException: Invalid IContextChannel passed to OperationContext. Must be either a server dispatching channel or a client proxy channel.. How do I mock, a server dispatching channel?

Original source