我正在使用SpecFlow和ASP.NET MVC编写测试,代码步骤非常好。但是,我想根据模型的验证编写测试。
在我的HomeController中(对于登录屏幕),Index()方法实现了两次,两者都有不同的方法签名:
// When the page is initially loaded, the value of null is passed to
// the parameter. This would prompt the view to load the login-screen.
public ActionResult Index(string u) {
// some declaration
return View(model);
}
[HttpPost, ValidateInput(false)]
public ActionResult Index(myobject o) {
// some declaration
return View(model);
}
// No default implementation for Index()
public string SomeSampleFunction() {
return "This is a test string";
}
当我编写测试时,对Index()方法的每个引用都返回null。我试过这样做:
var _controller = new HomeController();
var _result = _controller.Index("") as ViewResult;
// The succeeding line also returns null
// var _result = _controller.Index(string.empty) as ViewResult;
Assert.IsInstanceOf<ViewResult>(_result);
Assert.IsEmpty(((ViewResult)_result).ViewName);
Assert.AreEqual("Log In", homeController.ViewData["Title"], "Page title is wrong");
我错过了这个吗?对Index()方法的引用始终为null,但如果我调用普通方法(如“SomeSampleFunction”),则它可以正常工作。有帮助吗?