0

在我的 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); 
    }
4

2 回答 2

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"));
    }
于 2014-05-15T18:04:51.420 回答
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); 
    }

试试上面的

于 2013-11-07T01:08:13.157 回答