Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock WebOperationContext for unit testing?

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;
            }
        }
like image 303
GilliVilla Avatar asked Apr 17 '13 19:04

GilliVilla


3 Answers

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);
like image 113
Qi Luo Avatar answered Sep 27 '22 20:09

Qi Luo


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

like image 38
John_J Avatar answered Sep 27 '22 19:09

John_J


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

like image 40
Phil Carson Avatar answered Sep 27 '22 18:09

Phil Carson