8

我正在尝试为GetAwesomeResultsAsXml()以下 WCF Rest 服务编写单元测试(更多的集成测试)。
我如何处理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;
            }
        }
4

5 回答 5

9

我遇到了同样的问题。我想在没有任何 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);
于 2014-05-22T09:12:24.613 回答
3

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

首先,你必须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

于 2015-09-25T07:34:07.140 回答
2

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

由于它们不允许您模拟静态成员,您需要将调用包装到 webcontext.current。这是包装静态 mmember 并使用 moq 进行测试的示例:Mock static property with moq

于 2013-04-18T08:57:35.163 回答
1

如果您还没有使用 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);
            }
        }
于 2013-11-01T17:52:39.450 回答
0

为您的服务创建一个客户端,然后在客户端中处理 OperationContext:

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

有关如何使用此功能的更多信息,请参阅此答案

于 2021-05-24T19:09:17.530 回答