ASP.NET WebApi unit testing with Request.CreateResponse

后端 未结 5 1970
遇见更好的自我
遇见更好的自我 2020-12-04 06:42

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 generat

相关标签:
5条回答
  • 2020-12-04 07:00

    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.

    0 讨论(0)
  • 2020-12-04 07:10

    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());
    
    0 讨论(0)
  • 2020-12-04 07:15

    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.

    0 讨论(0)
  • 2020-12-04 07:17

    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.

    0 讨论(0)
  • 2020-12-04 07:18

    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.

    0 讨论(0)
提交回复
热议问题