【发布时间】:2013-06-25 04:10:24
【问题描述】:
我正在为这样的控制器编写单元测试:
public HttpResponseMessage PostLogin(LoginModel model)
{
if (!ModelState.IsValid)
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
模型看起来像:
public class LoginModel
{
[Required]
public string Username { set; get; }
[Required]
public string Password { set; get; }
}
然后我有这样的单元测试:
[TestMethod]
public void TestLogin_InvalidModel()
{
AccountController controller = CreateAccountController();
...
var response = controller.PostLogin(new LoginModel() { });
Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
}
实际上 ModelState 已经过验证...这对我来说很奇怪,因为这两个字段都是必需的... 有人可以建议吗?
【问题讨论】:
-
aspnetcore 见stackoverflow.com/questions/43229338/…
标签: c# unit-testing asp.net-web-api vs-unit-testing-framework model-validation