在不嘲讽的情况下测试测试项目中的视图
本文关键字:测试 项目 视图 情况下 | 更新日期: 2023-09-27 18:30:13
如何在不使用mocking的情况下从测试项目中测试视图?我试过各种各样的东西。通过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
这个错误发生在我使用实例化方法的时候。
当我尝试执行应用程序主机解决方案时,它失败了,并出现序列化异常,表示无法序列化属性。
我被卡住了——该怎么办?
您的视图在UI层中,您只需对UI层进行单元测试,模拟从服务层到控制器/视图使用的所有内容。
UI层不需要集成测试,因为测试团队测试用例(功能)是否正确实现会让您知道页面是否无法加载。
您的业务逻辑通常位于一个单独的层中,例如您的服务层。这是在服务层和数据层之间最常见的集成测试。
与其试图解决环境问题,为什么不坚持有效的方法呢?UI层和单元的单元测试以及服务层的集成测试。
除了进行自动浏览器测试外,您还可以使用类似Selenium或类似的东西。