【发布时间】:2016-04-27 17:37:38
【问题描述】:
尽管 StackOverflow 上有几篇关于 MVC 中单元测试操作结果的帖子,但我有一个具体的问题......
这是我在控制器中的 ActionResult:
public ActionResult Index()
{
return View(db.Products.ToList());
}
产品中的每个项目都有不同的属性,例如名称、照片、数量等。 我为这个方法写了一个测试方法。它看起来如下:
private CartEntity db = new CartEntity();
[TestMethod]
public void Test_Index()
{
//Arrange
ProductsController prodController = new ProductsController();
ViewResult = prodController.Index();
}
在这种情况下我应该比较什么,因为没有参数被传递到索引操作中
【问题讨论】:
-
根据您的代码
Index()返回ViewResult而不是RedirectToRouteResult所以我对您的单元测试代码和您的问题感到困惑。 -
嘿@Igor,谢谢..我的实际代码是错误的
-
Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));根据您的最新更新仍然会失败。Index仍然返回ViewResult -
1.您可以测试传递给
ViewResult的模型。 2.可以测试action方法名是空还是“Index”。 3.可以测试返回的类型是ViewResult类型。 -
你不是在模拟你的数据库,所以这不是一个单元测试,而是一个集成测试。你可以检查
db.Products.ToList()和routeResult.ViewData.Model
标签: c# asp.net asp.net-mvc unit-testing asp.net-mvc-4