這裏創建一個簡單的單元測試的麻煩是單元測試:在MVC3應用
[TestFixture]
public class HomeControllerTests
{
[Test]
public void Index_GET_Asks_For_Index_View()
{
HomeController controller = new HomeController();
ViewResult result = controller.Index();
Assert.AreEqual("Index", result.ViewName);
}
}
基本上我要確保當我訪問Index操作方法,我調用正確的觀點。但是,測試失敗,我不知道爲什麼。
------測試開始:大會:Demo.Tests.dll ------
測試 'Demo.Tests.HomeControllerTests.Index_GET_Asks_For_Index_View' 失敗:
期望的字符串長度5,但是0字符串在預期索引0
不同: 「索引」
但是:-----------^HomeControllerTests.cs(19,0) :在 Demo.Tests.HomeControllerTests.Index_GET_Asks_For_Index_View()
下面是實際的控制器代碼:
public ViewResult Index()
{
ViewBag.Message = "This is just a demo.";
return View();
}
它看起來像你的期望,你真的有一個字符串長度爲零,這使得'ViewName'看起來像一個空字符串。我的猜測是,在'ViewName'的分辨率空當,解析爲正確的'ViewName'在此之後。 – 2012-01-05 15:57:44
之後? -padding- – 2012-01-05 15:59:18
查看我的回答。 'ViewName'將是空的11次中有10 – 2012-01-05 16:01:39