T4MVC和MvcContrib.TestHelpers获得“相对虚拟路径”错误检测 [英] T4MVC and testing with MvcContrib.TestHelpers getting 'The relative virtual path' errors

查看:204
本文介绍了T4MVC和MvcContrib.TestHelpers获得“相对虚拟路径”错误检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题,我是因为我在MVC土地n00bness,但我试着写一个测试为在与T4MVC生成器的功能。所以,我想我会用MvcContrib.TestHelper为好。

My issues my be due to my n00bness in the MVC land, but I'm trying to write a test for a function in a controller that was generated with T4MVC. So I thought I'd use the MvcContrib.TestHelper as well.

我新建了一个TestControllerBuilder和我的控制器,当我尝试并调用InitializeController它我得到一个异常:

I new up a TestControllerBuilder and my controller, when I try and call InitializeController on it I get an exception:

System.TypeInitializationException:对影像的类型初始值引发了异常。 ---> System.Web.HttpException:应用程序相对虚拟路径'〜/内容/图像/ add.gif'不能进行绝对的,因为对应用程序的路径是未知的

System.TypeInitializationException: The type initializer for 'Images' threw an exception. ---> System.Web.HttpException: The application relative virtual path '~/Content/Images/add.gif' cannot be made absolute, because the path to the application is not known.

好吧,它试图让图像和他们不是在测试可执行文件的相对路径......我怎么知道呢?

Ok, it's trying to get the images and they are not in the relative path of the test executable...how do I tell it?

我想这T4MVCHelpers.ProcessVirtualPath(),但pretty太多的一切,我尝试在那里当它调用VirtualPathUtility.ToAbsolute()方法将引发错误。

I tried this T4MVCHelpers.ProcessVirtualPath() but pretty much everything I try in there throws an error when it calls the VirtualPathUtility.ToAbsolute() method.

我毫不怀疑,我做错了什么。我是不正确的我的理解是TestControllerBuiler将设置我的模拟HTTPContexts给我吗?我可以告诉它忽略图像的东西?

I have no doubt I'm doing something wrong. Am I incorrect in my understanding that the TestControllerBuiler will setup my mock HTTPContexts for me? Can I tell it to ignore the image stuff?

请帮助的n00b出来。关于如何使用TestHelper和ProcessVirtualPath将是真棒,一个简单的例子。

Please help a n00b out. A simple example on how to use the TestHelper and ProcessVirtualPath would be awesome.

更新

对于那些谁得到了类似的错误,我的理由是因为HttpContext的是在控制器构造函数中被使用。在我的情况下,它被示数T4MVC生成的强类型的图像路径​​。

For those who get a similar error, my reason was because the httpcontext was being used in the controller constructor. In my case it was erroring on a strong typed image path T4MVC generated.

推荐答案

您可以提供自己的方法T4MVC通过设置ProcessVirtualPath代表使用。

You can provide your own method for T4MVC to use by setting the ProcessVirtualPath delegate.

下面的匿名方法保持原来实行的精神。

The anonymous method below keeps the spirit of the original implementation.

T4MVCHelpers.ProcessVirtualPath = p => VirtualPathUtility.ToAbsolute(p, "/App");

这篇关于T4MVC和MvcContrib.TestHelpers获得“相对虚拟路径”错误检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆