152

I am trying to write some unit tests for my ApiController and faced some issues. There is a nice extension method called Request.CreateResponse that helps a lot with generating response.

public HttpResponseMessage Post(Product product)
{
  var createdProduct = repo.Add(product);
  return this.Request.CreateResponse(HttpStatusCode.Created, createdProduct);
}

Is there any way to mock CreateResponse without using of partial mocks or direct using of "new HttpResponseMessage(...)"?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
asa
  • 1,827
  • 2
  • 15
  • 18
  • 1
    Why do you want to mock the `CreateResponse`? Why not assert on the returned `HttpResponseMessage` `Content` and `StatusCode` properties that the correct values are set? – nemesv Jun 03 '12 at 07:48
  • 3
    If I run this method form unit tests, it will fail with exception that configuration is not set. – asa Jun 03 '12 at 08:01
  • 1
    Ok, shame on me, I just needed to write this.Request.CreateResponse(HttpStatusCode.Accepted, createdProduct, GlobalConfiguration.Configuration) – asa Jun 03 '12 at 08:03

5 Answers5

246

Another way to solve this is to do the following:

controller.Request = new HttpRequestMessage();
controller.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, 
                                  new HttpConfiguration());

If you are upgrading to webapi 5.0, then you'll need to change this to:

controller.Request = new HttpRequestMessage();
controller.Request.SetConfiguration(new HttpConfiguration());

The reason why you need to do this is because you have to have Request populated on the controller otherwise the extension methods on Request won't work. You also have to have an HttpConfiguration set on the Request otherwise routing and other parts of the pipeline won't function correctly.

SteveC
  • 15,808
  • 23
  • 102
  • 173
jonnii
  • 28,019
  • 8
  • 80
  • 108
  • Yes, that might work as well, but it seems Request.CreateResponse also work. – asa Jun 09 '12 at 04:35
  • 4
    Adding GlobalConfiguration.Configuration still left request null. Setting the Request worked for me, however this wasn't the end of my problems as my Action was also calling Url.Link and the route name was not defined. – foolshat Sep 10 '12 at 08:14
  • Help for setting up route http://stackoverflow.com/questions/11779311/testing-webapi-controller-url-link – foolshat Sep 10 '12 at 08:28
  • Setting the ControllerContext *after* this call gives me that error too, but only with WebApi 5.0.0. But if I set the HttpConfiguration object on the Request *last*, it works fine. – mikebridge Oct 28 '13 at 22:14
  • This works perfectly, but I fail to understand why this works. Could you elaborate this in the answer? – Jan_V Jul 17 '14 at 08:48
  • Suggested additional note for clarity: this code needs to be added to the unit test code (yes, obvious, but not always immediately clear on the first reading). – Aidanapword Aug 10 '15 at 09:56
24

You could set up the controller object for testability like this:

var config = new HttpConfiguration();
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/products");
var route = config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "products" } });

controller.ControllerContext = new HttpControllerContext(config, routeData, request);
controller.Request = request;
controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;

Copied from Peter Provost's comprehensive blog post on Unit Testing ASP.NET Web API.

mono68
  • 2,080
  • 19
  • 27
  • 1
    You may also be interested in the MS docs related to this: https://learn.microsoft.com/en-us/aspnet/web-api/overview/testing-and-debugging/unit-testing-controllers-in-web-api You may also need to add references to System.Web.Http and System.Net.Http – Aaron Hoffman Apr 13 '18 at 15:28
7

For Web API 2, you can simply add

controller.Request = new HttpRequestMessage();
controller.Configuration = new HttpConfiguration();

Like so

[TestMethod]
public void GetReturnsProduct()
{
    // Arrange
    var controller = new ProductsController(repository);
    controller.Request = new HttpRequestMessage();
    controller.Configuration = new HttpConfiguration();

    // Act
    var response = controller.Get(10);

    // Assert
    Product product;
    Assert.IsTrue(response.TryGetContentValue<Product>(out product));
    Assert.AreEqual(10, product.Id);
}

It's important to set Request and Configuration on the controller. Otherwise, the test will fail with an ArgumentNullException or InvalidOperationException.

See here for more info.

Dan Friedman
  • 4,941
  • 2
  • 41
  • 65
1

WebAPI 1 here with a similar issue using VB.

I managed to hybrid responses here to get this to work as simple as this:

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

Just posting in case it helps anyone.

Rudy Scoggins
  • 431
  • 3
  • 8
1

In your test class, create an instance of the controller class. e.g var customerController= new CustomerController();

customerController.Request = new HttpRequestMessage();
customerController.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
KiranC
  • 597
  • 6
  • 6