从测试项目中测试一个视图而不是嘲笑?

时间:2012-04-07 04:03:22

标签: c# asp.net-mvc unit-testing

如何在不使用模拟的情况下从测试项目中测试我的视图?我尝试过各种各样的事情。通过MarshalByRefObject创建ASP .NET应用程序主机并使用其ViewToString方法。我也尝试动态地实例化一个ViewPage对象,没有运气。

Test method Fablelane.WebApplication.Tests.UseCases.CreateNewSchoolAsAdministrator threw exception: 
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Web.VirtualPath.GetCacheKey()
at System.Web.Compilation.BuildManager.GetCacheKeyFromVirtualPath(VirtualPath virtualPath, ref Boolean keyFromVPP)
at System.Web.Compilation.BuildManager.GetVPathBuildResultFromCacheInternal(VirtualPath virtualPath, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound)
at System.Web.Compilation.BuildManager.GetObjectFactory(String virtualPath, Boolean throwIfNotFound)
at System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.FileExists(String virtualPath)
at System.Web.Mvc.BuildManagerViewEngine.FileExists(ControllerContext controllerContext, String virtualPath)
at System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromGeneralName(ControllerContext controllerContext, List`1 locations, String name, String controllerName, String areaName, String cacheKey, String[]& searchedLocations)
at System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[] locations, String[] areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String[]& searchedLocations)
at System.Web.Mvc.VirtualPathProviderViewEngine.FindView(ControllerContext controllerContext, String viewName, String masterName, Boolean useCache)
at System.Web.Mvc.ViewEngineCollection.<>c__DisplayClassc.<FindView>b__b(IViewEngine e)
at System.Web.Mvc.ViewEngineCollection.Find(Func`2 lookup, Boolean trackSearchedPaths)
at System.Web.Mvc.ViewEngineCollection.Find(Func`2 cacheLocator, Func`2 locator)
at System.Web.Mvc.ViewEngineCollection.FindView(ControllerContext controllerContext, String viewName, String masterName)
at Fablelane.WebApplication.Tests.Extensions.ControllerExtensions.RenderPartialToString(ControllerBase controller, String partialName, Object model, RouteData routeData) in ControllerExtensions.cs: line 28
at Fablelane.WebApplication.Tests.UseCases.InvokeView(Controller controller, ViewResult result) in UseCases.cs: line 104
at Fablelane.WebApplication.Tests.UseCases.CreateNewSchoolAsAdministrator() in UseCases.cs: line 226

当我使用实例化方法时,该错误。

当我尝试执行Application Host解决方案时,它失败并出现序列化异常,说明属性无法序列化。

我被困 - 怎么办?

1 个答案:

答案 0 :(得分:1)

您的视图位于您的UI图层中,您只需对UI图层进行单元测试,模拟服务层以及控制器/视图所使用的所有内容。

您的UI层不需要集成测试,因为测试团队测试您的用例(功能)是否正确实现会让您知道页面是否无法加载。

您的业务逻辑通常位于单独的层中,例如您的服务层。 这是服务和数据层之间最常见的集成测试。

而不是试图解决环境问题,为什么不坚持使用什么有效? UI层和单元的单元测试以及服务层的集成测试。

除了自动浏览器测试之外,您还可以使用Selenium或类似内容。