我正在尝试为以下 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;
}
}
首先,你必须
// 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.aspxusing (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...
}
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);
}
}
有关如何使用此功能的更多信息,请参阅此答案
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();
}
}
}