且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#ASP.NET MVC控制器单元测试

更新时间:2023-02-17 07:44:41

下面是一种方法,你可以做到这一点。您也可以验证是否根据您的机型。

Here is a way you can do it. You can also verify based on your model type

[TestMethod]
public void TestMethod2()
{
  MessageController controller = new MessageController();
  ActionResult result = controller.Index(1);
  Assert.IsInstanceOfType(result, typeof(ViewResult));
  //Since view has been asserted as ViewResult
  ViewResult viewResult = result as ViewResult;  
  if(viewResult != null)
  {      
     Assert.IsInstanceOfType(viewResult.Model, typeof(YourModelType));
    //Further Asserts for your model 
  } 
}