我正在为控制器进行单元测试,这是我的代码。
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);
}
答案 0 :(得分:0)
[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);
}