我正在为控制器进行单元测试,这是我的代码。ASP.net MVC测试版Beta 1
public void DocumentController_IndexMethod_ShouldReturn_Documents()
{
DocumentsController c = new DocumentsController(_repository);
ViewResult result = (ViewResult)c.Index("1");
DocumentsController.DocumentsData data = (DocumentsController.DocumentsData)result.ViewData;
Assert.IsNotNull(data.Documents);
Assert.IsTrue(data.Documents.Count() > 0);
Assert.IsNotNull(result);
}
我基本上跟随着Rob Conery的asp.net店面应用程序,并意识到我不能使用RenderView方法。如图所示,我尝试了ViewResult方法来创建视图的一个实例。我收到此错误: 错误1无法将类型'System.Web.Mvc.ViewDataDictionary'转换为'HomeOwners.Controllers.DocumentsController.DocumentsData'C:\ Documents and Settings \ drmarshall \ My Documents \ Visual Studio 2008 \ Projects \ HomeOwners \ HomeOwners.Tests \ DocumentsControllerTests.cs 61 54 HomeOwners.Tests
我是使用正确的替换方法,还是我错过了什么?
我想通了。
[TestMethod]
public void DocumentController_IndexMethod_ShouldReturn_Documents()
{
DocumentsController c = new DocumentsController(_repository);
ViewResult result = c.Index("1") as ViewResult;
ViewDataDictionary dictionary = result.ViewData;
DocumentsController.DocumentsData data = (DocumentsController.DocumentsData)dictionary["Documents"];
Assert.IsNotNull(data.Documents);
Assert.IsTrue(data.Documents.Count() > 0);
Assert.IsNotNull(result);
}
如果在这里有一个问题,我不能告诉它是什么。 – 2009-02-04 16:53:52