T4MVC 和使用 MvcContrib.TestHelpers 进行测试获取“相对虚拟路径”;错误
我的问题是由于我对 MVC 领域的了解所致,但我正在尝试为使用 T4MVC 生成的控制器中的函数编写测试。所以我想我也可以使用 MvcContrib.TestHelper 。
我新建了一个 TestControllerBuilder 和我的控制器,当我尝试在其上调用 InitializeController 时,出现异常:
System.TypeInitializationException:“图像”的类型初始值设定项引发异常。 ---> System.Web.HttpException:应用程序相对虚拟路径“~/Content/Images/add.gif”无法设置为绝对路径,因为应用程序的路径未知。
好的,它正在尝试获取图像,但它们不在测试可执行文件的相对路径中......我该如何告诉它?
我尝试了这个 T4MVCHelpers.ProcessVirtualPath() 但几乎我在那里尝试的所有内容在调用 VirtualPathUtility.ToAbsolute() 方法时都会抛出错误。
我毫不怀疑我做错了什么。我对 TestControllerBuiler 将为我设置模拟 HTTPContexts 的理解是否错误?我可以告诉它忽略图像内容吗?
请帮助n00b。关于如何使用 TestHelper 和 ProcessVirtualPath 的简单示例将非常棒。
更新
对于那些遇到类似错误的人,我的原因是因为在控制器构造函数中使用了 httpcontext。就我而言,它在 T4MVC 生成的强类型图像路径上出错。
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.
I new up a TestControllerBuilder and my controller, when I try and call InitializeController on it I get an exception:
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?
I tried this T4MVCHelpers.ProcessVirtualPath() but pretty much everything I try in there throws an error when it calls the VirtualPathUtility.ToAbsolute() method.
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?
Please help a n00b out. A simple example on how to use the TestHelper and ProcessVirtualPath would be awesome.
Update
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过设置 ProcessVirtualPath 委托来提供您自己的方法供 T4MVC 使用。
下面的匿名方法保留了原始实现的精神。
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.