我正在尝试为以下WCF休息服务的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;
}
}
答案 0 :(得分:6)
我遇到了同样的问题。我想在没有任何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);
答案 1 :(得分:3)
我按照Sanjay的回答,尝试了MS假框架,
首先,您必须open "Solution Explorer > your test project > Reference"
=&gt; right-click the "System.ServiceModel.Web"
=&gt; 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
答案 2 :(得分:1)
常见的方法是使用moq(https://code.google.com/p/moq/)或rhinomocks等模拟工具。
由于它们不允许您模拟静态成员,因此需要将调用包装到webcontext.current。下面是一个包装静态mmember并使用moq进行测试的示例:Mock static property with moq
答案 3 :(得分: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);
}
}
答案 4 :(得分: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();
}
}
}
有关如何使用它的更多信息,请参阅this answer。