如何模拟WebOperationContext进行单元测试
本文关键字:WebOperationContext 单元测试 模拟 何模拟 | 更新日期: 2023-09-27 18:09:36
我正在尝试为以下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 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...
}
,现在你可以得到WebOperationContext.Current.IncomingRequest。头文件["myCustomHeader"]现在在你的WCF服务代码。
关于MSDN上的MS Fakes框架的更多信息:https://msdn.microsoft.com/en-us/library/hh549176.aspx
一种常见的方法是mock工具,如moq (https://code.google.com/p/moq/)或rhinomocks。
因为它们不允许你模拟静态成员,所以你需要包装对webcontext.current的调用。下面是一个用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);
}
}
为您的服务创建一个客户端,然后在客户端中处理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();
}
}
}