简体   繁体   English

Asp.Net MVC 项目中的单元测试

[英]Unit Testing in Asp.Net MVC project

I am a newbie to write a unit testing please provide the guidance on what is the unit test for the following simple structure which may be passed in TestResult in VS 2019???我是一个写单元测试的新手,请提供有关以下简单结构的单元测试的指导,这些简单结构可能在 VS 2019 的 TestResult 中通过???

public ActionResult About()
{
    ViewBag.Message = "Your application description page.";
    return View();
}

Based on the shown example, inspect the view result to assert the expected behavior when About is invoked.根据显示的示例,检查视图结果以在调用About时断言预期的行为。

A very simplified example一个非常简化的例子

[TestMethod]
public void AboutTest() {
    //Arrange
    var controller = new MyController();
    string expected = "Put expected message here";

    //Act
    var result = controller.About() as ViewResult;
    var actual = (string) result.ViewData["Message"];

    //Assert
    Assert.AreEqual(expected, actual);
}

The ViewBag data can be extracted from the ViewResult.ViewData collection as shown above. ViewBag数据可以从ViewResult.ViewData集合中提取,如上所示。

Note the use of the matching name of the dynamic property set in the controller action.注意在控制器动作中使用动态属性集的匹配名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM