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(…)”?
Another way to solve this is to do the following:
If you are upgrading to webapi 5.0, then you’ll need to change this to:
The reason why you need to do this is because you have to have
Requestpopulated on the controller otherwise the extension methods onRequestwon’t work. You also have to have anHttpConfigurationset on the Request otherwise routing and other parts of the pipeline won’t function correctly.