简体   繁体   中英

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;
            }
        }

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);

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

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

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);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM