c# - 在不模拟的情况下测试测试项目的 View ?

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

如何在不使用模拟的情况下从我的测试项目中测试我的 View ?我尝试过各种各样的事情。通过 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

那个错误是我使用实例化方法的时候。

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

我被卡住了 - 怎么办?

最佳答案

您的 View 位于您的 UI 层中,您只需对 UI 层进行单元测试,模拟从服务层到您的 Controller / View 正在使用的所有内容。

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

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

与其尝试解决环境问题,不如坚持做有效的事情? UI 层和单元的单元测试以及服务层的集成测试。

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

关于c# - 在不模拟的情况下测试测试项目的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10051635/

相关文章:

c# - 如何从 Visual Studio 调试输出窗口读取文本

c# - Log4NET、ADONetAppender 和自定义字段

javascript - 使用 Html.BeginCollectionItem 帮助程序传递集合的分部 View

asp.net-mvc - 对 Silverlight 如何进入页面感到困惑

c# - 在单独的线程上引发事件

c# - 处理验证方法错误消息的好方法

c# - 让 ServiceStack 保留类型信息

c# - 单元测试,确保良好的覆盖率,同时避免不必要的测试

c# - IQueryable 单元或集成测试

python - 方法Python中的测试方法