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

问题描述 投票:0回答:6

我正在尝试为以下 WCF Rest 服务编写一个单元测试(更多的是集成测试)。

我如何处理
GetAwesomeResultsAsXml()
嘲笑方面?
最好的方法是什么?  

WebOperationContext

	
c# wcf unit-testing rest weboperationcontext
6个回答
10
投票

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



3
投票

首先,你必须

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

=>

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;

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

MSDN 上有关 MS Fakes 框架的更多信息:

https://msdn.microsoft.com/en-us/library/hh549176.aspx


2
投票
https://code.google.com/p/moq/

) 或 rhinomocks 等模拟工具。 由于它们不允许您模拟静态成员,因此您需要将调用包装到 webcontext.current。 以下是包装静态 mmmember 并使用起订量进行测试的示例:

使用起订量模拟静态属性


1
投票

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



0
投票

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

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


0
投票

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

	
© www.soinside.com 2019 - 2024. All rights reserved.