【发布时间】:2012-06-07 18:44:00
【问题描述】:
我正在尝试为我的 ApiController 编写一些单元测试,但遇到了一些问题。有一个很好的扩展方法,叫做 Request.CreateResponse,它对生成响应有很大帮助。
public HttpResponseMessage Post(Product product)
{
var createdProduct = repo.Add(product);
return this.Request.CreateResponse(HttpStatusCode.Created, createdProduct);
}
有没有办法在不使用部分模拟或直接使用“new HttpResponseMessage(...)”的情况下模拟 CreateResponse?
【问题讨论】:
-
为什么要模拟
CreateResponse?为什么不对返回的HttpResponseMessageContent和StatusCode属性断言设置了正确的值? -
如果我以单元测试的形式运行此方法,它将失败,但未设置配置。
-
好吧,真丢人,我只需要写 this.Request.CreateResponse(HttpStatusCode.Accepted, createdProduct, GlobalConfiguration.Configuration)
标签: c# unit-testing asp.net-web-api mocking httpresponse