2

如何在不使用模拟的情况下从我的测试项目中测试我的视图?我尝试过各种各样的事情。通过 MarshalByRefObject 并使用其 ViewToString 方法创建 ASP .NET 应用程序宿主。我也尝试过动态实例化 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

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

当我尝试执行应用程序主机解决方案时,它失败并出现序列化异常,指出无法序列化属性。

我被困住了——该怎么办?

4

1 回答 1

1

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

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

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

与其尝试解决环境问题,为什么不坚持有效的方法?UI 层和单元的单元测试以及服务层的集成测试。

除了自动化浏览器测试,您还可以使用Selenium或类似的东西。

于 2012-04-07T04:43:20.503 回答