Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Data Services: Mocking OperationContext

We would like to mock the OperationContext class for testing purposes. We are using 'Mock'. But OperationContext is a sealed class and cannot be mocked. Therefore we are trying to create a dummy OperationContext object. But OperationContext constructor takes IContextChannel as parameter. We would like to know of a way to get hold of a IContextChannel so that it can be passed to the OperationContext constructor.

like image 935
Satyaprakash J Avatar asked Feb 16 '11 12:02

Satyaprakash J


1 Answers

The general recommendation is to implement wcf services without referencing wcf.

Examples how to do it are in http://marcin.floryan.pl/blog/2012/01/do-we-really-need-wcfmock

We replaced a reference to OperationContext with a reference to operationResponse. The operationResponse should be defined as an interface and can be injected in the constructor just like the repository is in the example given. Our test method now becomes slightly shorter and more readable.

And  in http://blogs.msdn.com/b/ploeh/archive/2006/12/04/integrationtestingwcfservices.aspx

Let's, for a moment, consider the need to perform authorization. You could inspect OperationContext.Current directly in each of your operation implementations, but that would be mixing concerns (business logic implemented in the operation mixed together with authorization). The correct way would be to provide a class deriving from ServiceAuthorizationManager, and configure the service to use this class for authorization. This would allow you to keep unit testing your operation implementations, but obviously, you also need to test the authorization manager itself, and it turns out that integration testing is the easiest way to accomplish this task.

If you still need mock follow a recommendation from http://blogs.msdn.com/b/ploeh/archive/2008/06/28/unit-testing-duplex-wcf-services.aspx

 

All you have to do is to replace the call toOperationContext.GetCallbackChannel with something abstract. On .NET 3.5, the easiest abstraction is Func, which has the same signature, but if you are on .NET 3.0, you can always define a similar delegate type of your own.

Alternatively as a last recourse  you can use WCFmock

#if DEBUG
     using WebOperationContext = System.ServiceModel.Web.MockedWebOperationContext;
#endif

This is useful for instance, if you want to use the mocked version in development, and always the WCF version in production. That's all, you do not need to touch your existing service implementation at all, once you defined that alias, the service is ready to be tested

From http://blog.gfader.com/2010/08/how-to-unit-test-wcf-service.html

all your calls to OperationContext.Current.Channel.SessionId get intercepted by MockedOperationContext, but only in your tests you are mocking the OperationContext. In your normal environment that MockedOperationContext acts as a proxy to the real OperationContext.

like image 156
Michael Freidgeim Avatar answered Nov 08 '22 16:11

Michael Freidgeim