ViewResult result = controller.Index() as ViewResult;
// Assert
ViewDataDictionary viewData = result.ViewData;
Assert.AreEqual("Welcome to ASP.NET MVC!", viewData["Message"]);
}
我们可以在测试代码中对viewData的内容进行验证,一般并不推荐使用弱类型的viewData来进行传递数据,下面我们来看一下如何来测试viewModel中的内容。
Code
public ActionResult About()
{
News news = new News() { ID = 1, Author = "lfm" };
return View(news);
}
测试代码
[TestMethod]
public void About()
{
// Arrange
HomeController controller = new HomeController();
// Act
ViewResult result = controller.About() as ViewResult;
Assert.AreEqual(1, ((News)result.ViewData.Model).ID);
// Assert
文章来源于领测软件测试网 https://www.ltesting.net/