Assert.AreEqual失败。预期:其中(空)取代。实际:<system.web.mvc.viewresult> </system.web.mvc.viewresult>

时间:2011-04-20 12:25:16

标签: asp.net-mvc-3

我正在asp.net mvc3上进行单元测试。当我在默认创建的测试上运行测试方法时:

[TestMethod()]
        public void IndexTest()
        {
            ConferenceController target = new ConferenceController(); // TODO: Initialize to an appropriate value
            ActionResult expected = Index; // TODO: Initialize to an appropriate value
            ActionResult actual;
            actual = target.Index();
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        } 

[TestMethod()]发生此错误:

  

Assert.AreEqual失败。预期:其中(空)取代。实际:

如何传递断言?

1 个答案:

答案 0 :(得分:2)

ActionResult expected = Index; // TODO: Initialize to an appropriate value

正如评论所示,您应该将Index变量初始化为适当的值。例如:

[TestMethod]
public void Index()
{
    // Arrange
    HomeController controller = new HomeController();

    // Act
    ViewResult result = controller.Index() as ViewResult;

    // Assert
    Assert.AreEqual("Welcome to ASP.NET MVC!", result.ViewBag.Message);
}