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???
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
CodePudding user response:
Based on the shown example, inspect the view result to assert the expected behavior when About
is invoked.
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.
Note the use of the matching name of the dynamic property set in the controller action.