繁体   English   中英

如何模拟WebOperationContext进行单元测试?

[英]How to mock WebOperationContext for unit testing?

我正在尝试为以下WCF Rest服务为GetAwesomeResultsAsXml()编写单元测试(更多的集成测试)。
我如何处理WebOperationContext模拟方面?
什么是最好的方法?

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

我遇到了同样的问题。 我想在没有任何IIS的情况下对WCF服务功能(对于IOauth2接口,如下例)进行单元测试。 这是准备的代码片段。

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

我按照Sanjay的回答,尝试了MS假框架,

首先,您必须open "Solution Explorer > your test project > Reference" => right-click the "System.ServiceModel.Web" => press "add Fakes Assembly"

参考:

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

样品:

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

现在您可以在WCF服务代码中获取WebOperationContext.Current.IncomingRequest.Headers [“myCustomHeader”]。

有关MSDN上的MS Fakes框架的更多信息: https//msdn.microsoft.com/en-us/library/hh549176.aspx

一种常见的方法是模拟工具,如moq( https://code.google.com/p/moq/ )或rhinomocks。

由于它们不允许您模拟静态成员,因此需要将调用包装到webcontext.current。 下面是一个包装静态mmember并使用moq进行测试的示例:使用moq 模拟静态属性

如果你还没有使用MS Fakes框架,可能有点矫枉过正,但是如果你这样做对我有用。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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