How to make fun of CreateResponse <T> extension method in HttpRequestMessage

I am using ASP.Net MVC 4 RC ApiController and am trying to do unit testing using the GET method.

This method uses the CreateResponse<T> method that is used in HttpRequestMessage , but I have no idea how to model it or make it work correctly.

The body of the method contains this:

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

In 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); 

But it just throws an InvalidOperationException :

The request has no associated configuration object, or the provided configuration was null.

Has anyone got any pointers on how I can configure HttpRequestMessage so that the CreateResponse really CreateResponse work?

+68
c # unit-testing asp.net-mvc-4 mocking
Jun 15 2018-12-18T00:
source share
2 answers

This was resolved by specifying an empty configuration:

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

I got an answer from here.

Testing the ASP.NET WebApi Module Using Request.CreateResponse

+157
Jun 15 '12 at 16:57
source

WebAPI 1 with VB is here!

I managed to get the hybrid answers from your link above to make it work as simple as this:

 Dim request As HttpRequestMessage = New HttpRequestMessage() Return request.CreateResponse(HttpStatusCode.BadRequest, myCustomClassObject, GlobalConfiguration.Configuration) 

Just publish in case this helps someone.

0
Mar 15 '17 at 15:37
source



All Articles