How to mock WebOperationContext for unit testing?

6.8k Views Asked by At

I am trying to get a unit test(more of an integration test) written for the GetAwesomeResultsAsXml() for the following WCF Rest Service.
How do I deal with the WebOperationContext mocking aspect?
What would be the best approach?

public class AwesomeRestService : AwesomeRestServiceBase, IAwesomeRestService
    {
        public AwesomeSearchResults<AwesomeProductBase> GetAwesomeResultsAsXml()
        {
            return GetResults();
        }

        private static AwesomeSearchResults<AwesomeProductBase> GetResults()
        {
            var searchContext = AwesomeSearchContext
                               .Parse(WebOperationContext.Current);
            ..............
            ..............
            ..............
        }


    }

[ServiceContract]
    public interface IAwesomeRestService
    {
        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml,
        BodyStyle =  WebMessageBodyStyle.Bare,
            UriTemplate = "/search/xml")]
        AwesomeQueryResults<AwesomeProductBase> GetAwesomeResultsAsXml();


    }







public class AwesomeSearchContext
        {
            ................
            ................
            ................
             public static AwesomeSearchContext Parse 
                                           (WebOperationContext operationContext)
            {
                return WebOperationContext.Current != null ? new     
 AwesomeSearchContext(operationContext.IncomingRequest.UriTemplateMatch.QueryParameters) : null;
            }
        }
5

There are 5 best solutions below

0
On

Create a client for your service, and then handle the OperationContext within the client:

public class AwesomeRestServiceClient : ClientBase<IAwesomeRestService>, IAwesomeRestService
{
    public class AwesomeRestServiceClient(string address)
        : base(new WebHttpBinding(), new EndpointAddress(address))
    {   
        this.Endpoint.EndpointBehaviors.Add(new WebHttpBehavior());
    }

    public AwesomeSearchResults<AwesomeProductBase> GetAwesomeResultsAsXml()
    {
        using (new OperationContextScope(this.InnerChannel))
        {
            return base.Channel.GetAwesomeResultsAsXml();
        }
    }
}

For further info on how to use this, see this answer.

1
On

I met the same problem. I want to unit test a WCF service function (for IOauth2 interface as below example) without any IIS. This is the code snippet for the preparation.

// Prepare WebOperationContext
var factory = new ChannelFactory<IOauth2>(
    new WebHttpBinding(),
    new EndpointAddress("http://localhost:80"));

OperationContext.Current = new OperationContext(factory.CreateChannel() as IContextChannel);
Debug.Assert(WebOperationContext.Current != null);
1
On

Likely overkill if you are not already using the MS Fakes framework, but if you are this works for me.

using (ShimsContext.Create())
        {

            var response = new ShimOutgoingWebResponseContext();
            var ctx = new ShimWebOperationContext
            {
                OutgoingResponseGet = () => response
            };

            ShimWebOperationContext.CurrentGet = () => ctx;

            try
            {
                ParameterInspector.BeforeCall("operationName", new string[]{"some_argument"} );
            }
            catch (Exception e)
            {
                Assert.IsNull(e);
            }
        }
0
On

A common approach to this is mocking tool like moq (https://code.google.com/p/moq/) or rhinomocks.

As they don't allow you to mock static members you would need to wrap the call to webcontext.current. Here is an example of wrapping a static mmember and testing with moq: Mock static property with moq

4
On

I followed Sanjay's answer, and try the MS fake framework,

First of all, you have to open "Solution Explorer > your test project > Reference" => right-click the "System.ServiceModel.Web" => press "add Fakes Assembly"

Reference:

using Microsoft.QualityTools.Testing.Fakes;
using System.ServiceModel.Web.Fakes;

Sample:

using (ShimsContext.Create())
{
    var response = new ShimOutgoingWebResponseContext();
    var request = new ShimIncomingWebRequestContext();

    var ctx_hd = new WebHeaderCollection();
    ctx_hd.Add("myCustomHeader", "XXXX");
    request.HeadersGet = () => ctx_hd;

    var ctx = new ShimWebOperationContext
    {
        OutgoingResponseGet = () => response,
        IncomingRequestGet = () => request
    };
    ShimWebOperationContext.CurrentGet = () => ctx;

    //Test your code here...
}

and now you can get the WebOperationContext.Current.IncomingRequest.Headers["myCustomHeader"] in your WCF service code now.

More about MS Fakes framework on MSDN: https://msdn.microsoft.com/en-us/library/hh549176.aspx