如何嘲笑CreateResponse< T>在Htt的prequestMessage扩展方法 [英] How to mock the CreateResponse<T> extension method on HttpRequestMessage

查看:283
本文介绍了如何嘲笑CreateResponse< T>在Htt的prequestMessage扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.Net MVC 4 RC的ApiController,我试图单元测试get方法。

I'm using ASP.Net MVC 4 RC's ApiController and I'm trying to unit test a Get method.

此方法使用CreateResponse方法是在Htt的prequestMessage,但我不知道如何嘲笑这或使之正常工作。

This method uses the CreateResponse method that's on the HttpRequestMessage, but I've no idea how to mock this or to make it function correctly.

该方法的主体包含这样的:

The method's body contains this:

MediaTypeHeaderValue header = new MediaTypeHeaderValue(versionedSmartBlock.ContentType);
var response = Request.CreateResponse<SmartBlock>(HttpStatusCode.OK, versionedSmartBlock, header);

在我的单元测试,我创建一个空的Htt prequestMessage:

Within my unit test, I create an empty 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);

但它只是产生一个InvalidOperationException:

But it just generates an InvalidOperationException:

该请求没有关联的配置对象或提供的配置为null。

The request does not have an associated configuration object or the provided configuration was null.

有没有人有我如何能配置任何指针型Htt prequestMessage从而使CreateResponse方法实际上它的工作?

Has anyone got any pointers on how I can configure the HttpRequestMessage so that the CreateResponse method actually does its job?

推荐答案

这是通过指定一个空的配置解决:

This was solved by specifying an empty configuration:

request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());

我得到这个问题的答案从这里

I got the answer to that from here

<一个href=\"http://stackoverflow.com/questions/10868673/asp-net-webapi-unit-testing-with-request-createresponse\">ASP.NET与Request.CreateResponse

这篇关于如何嘲笑CreateResponse&LT; T&GT;在Htt的prequestMessage扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆