Gil*_*lla 6 c# rest wcf unit-testing weboperationcontext
我正在尝试GetAwesomeResultsAsXml()为以下WCF Rest服务编写单元测试(更多的集成测试).
我如何处理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;
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题.我想在没有任何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);
Run Code Online (Sandbox Code Playgroud)
一种常见的方法是模拟工具,例如 moq ( https://code.google.com/p/moq/ ) 或 rhinomocks。
由于它们不允许您模拟静态成员,因此您需要将调用包装到 webcontext.current。以下是包装静态 mmmember 并使用起订量进行测试的示例:使用起订量模拟静态属性
| 归档时间: |
|
| 查看次数: |
4670 次 |
| 最近记录: |