Skip to content

Instantly share code, notes, and snippets.

@FernandoVezzali
Created September 16, 2013 15:41
Show Gist options
  • Save FernandoVezzali/6582368 to your computer and use it in GitHub Desktop.
Save FernandoVezzali/6582368 to your computer and use it in GitHub Desktop.
Unit Testing ASP.NET Web API
//http://www.peterprovost.org/blog/2012/06/16/unit-testing-asp-dot-net-web-api/
private static void SetupControllerForTests(ApiController controller)
{
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment