Tom*_*ess 69 c# unit-testing mocking asp.net-mvc-4
我正在使用ASP.Net MVC 4 RC的ApiController,我正在尝试对GET方法进行单元测试.
这个方法使用的CreateResponse<T>方法HttpRequestMessage,但我不知道如何模拟它或使其正常运行.
该方法的主体包含:
MediaTypeHeaderValue header = new MediaTypeHeaderValue(versionedSmartBlock.ContentType);
var response = Request.CreateResponse<SmartBlock>(
HttpStatusCode.OK, versionedSmartBlock, header);
Run Code Online (Sandbox Code Playgroud)
在我的单元测试中,我创建一个空的HttpRequestMessage:
CallsController api = new CallsController(
managerMock.Object, config, adapterFactoryMock.Object);
api.Request = new HttpRequestMessage(
HttpMethod.Get, "http://localhost/Initiate?ern=%2B44123456789");
var response = api.Get("+44123456789", null);
Run Code Online (Sandbox Code Playgroud)
但它只会产生一个InvalidOperationException:
请求没有关联的配置对象,或者提供的配置为null.
有没有人有关于我如何配置的任何指示,HttpRequestMessage以便该CreateResponse方法实际上发挥作用?
Tom*_*ess 159
通过指定空配置解决了这个问题:
request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
Run Code Online (Sandbox Code Playgroud)
我从这里得到了答案
使用Request.CreateResponse进行ASP.NET WebApi单元测试
| 归档时间: |
|
| 查看次数: |
16900 次 |
| 最近记录: |