Ich versuche, einige Komponententests für meinen ApiController zu schreiben, und habe einige Probleme gehabt. Es gibt eine Nice-Erweiterungsmethode namens Request.CreateResponse, die beim Generieren von Antworten sehr hilfreich ist.
public HttpResponseMessage Post(Product product)
{
var createdProduct = repo.Add(product);
return this.Request.CreateResponse(HttpStatusCode.Created, createdProduct);
}
Gibt es eine Möglichkeit, CreateResponse ohne Partial-Mocks oder die direkte Verwendung von "new HttpResponseMessage (...)" zu verspotten?
Eine andere Möglichkeit, dies zu lösen, besteht darin, Folgendes zu tun:
controller.Request = new HttpRequestMessage();
controller.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey,
new HttpConfiguration());
Wenn Sie ein Upgrade auf Webapi 5.0 durchführen, müssen Sie Folgendes ändern:
controller.Request = new HttpRequestMessage();
controller.Request.SetConfiguration(new HttpConfiguration());
Der Grund, warum Sie dies tun müssen, ist, dass Sie Request
auf dem Controller haben müssen, da sonst die Erweiterungsmethoden für Request
nicht funktionieren. Sie müssen außerdem eine HttpConfiguration
für die Anforderung festgelegt haben, da sonst das Routing und andere Teile der Pipeline nicht ordnungsgemäß funktionieren.
Sie können das Controller-Objekt wie folgt auf Testbarkeit einrichten:
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;
Aus dem umfassenden Blogbeitrag von Peter Provost unter Unit Testing ASP.NET Web API .
WebAPI 1 hier mit einem ähnlichen Problem bei der Verwendung von VB.
Es gelang mir, die Antworten hier zu kombinieren, damit dies so einfach funktioniert:
Dim request As HttpRequestMessage = New HttpRequestMessage()
Return request.CreateResponse(HttpStatusCode.BadRequest, myCustomClassObject, GlobalConfiguration.Configuration)
Einfach posten, falls es jemandem hilft.
Für Web API 2 können Sie einfach hinzufügen
controller.Request = new HttpRequestMessage();
controller.Configuration = new HttpConfiguration();
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);
}
Es ist wichtig, Request und Configuration im Controller einzustellen. Andernfalls schlägt der Test mit einer ArgumentNullException oder InvalidOperationException fehl.
hier für weitere Informationen.
Erstellen Sie in Ihrer Testklasse eine Instanz der Controller-Klasse . z.B. var customerController= new CustomerController();
customerController.Request = new HttpRequestMessage();
customerController.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());