我是 mvc4 和 TDD 的新手。
当我尝试运行此测试时,它失败了,我不知道为什么。我已经尝试了很多事情,我开始在圈子里跑来跑去。
// GET api/User/5
[HttpGet]
public HttpResponseMessage GetUserById (int id)
{
var user = db.Users.Find(id);
if (user == null)
{
//return Request.CreateResponse(HttpStatusCode.NotFound);
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return Request.CreateResponse(HttpStatusCode.OK, user);
}
[TestMethod]
public void GetUserById()
{
//Arrange
UserController ctrl = new UserController();
//Act
var result = ctrl.GetUserById(1337);
//Assert
Assert.IsNotNull(result);
Assert.AreEqual(HttpStatusCode.NotFound,result.StatusCode);
}
结果:
Test method Project.Tests.Controllers.UserControllerTest.GetUserById threw exception:
System.ArgumentNullException: Value cannot be null. Parameter name: request