如果部分视图编译,如何进行单元测试?

时间:2013-11-07 00:38:30

标签: c# asp.net-mvc unit-testing asp.net-mvc-4

在我的Index.cshtml页面中,我有部分视图,我想进行单元测试。这是我对Index.cshtml的代码。如何制作部分视图?

    [TestMethod]
    public void IndexUnitTest()
    {
        // Arrange 
        InspectionController controller = new InspectionController();
        // Act 
        ViewResult result = controller.Index("stringHere") as ViewResult;

        // Assert 
        Assert.IsNotNull(result);
        Assert.IsNotNull(result.ViewName); 
    }

2 个答案:

答案 0 :(得分:0)

[TestMethod]
    public void PartialViewUnitTest()
    {
        // Arrange 
        InspectionController controller = new InspectionController();
        // Act 
        PartialViewResult result = controller.SomePartialView("stringHere") as PartialViewResult;

        // Assert 
        Assert.IsNotNull(result);
        Assert.IsNotNull(result.ViewName); 
    }

试试上面的

答案 1 :(得分:0)

如果您想测试,部分视图返回的内容没有moqing,您可以执行以下操作。但是,莫青将是这样做的正确方法。

[TestMethod]
    public void Test()
    {
      PartialViewResult response = controller.GetList(TestSearch) as PartialViewResult;


      Assert.AreEqual(response.ViewName,"_GetList");

      //Converting partial view to json string
      JavaScriptSerializer serializer = new JavaScriptSerializer(); //creating serializer instance of JavaScriptSerializer class

      StringBuilder builder = new StringBuilder();
      serializer.Serialize((object)response.Model, builder);

      //Checking in partial view
      Assert.IsTrue(builder.ToString().Contains("\"Success\":true"));
    }