簡體   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