T4MVC and testing with MvcContrib.TestHelpers getting 'The relative virtual path' errors

不打扰是莪最后的温柔 提交于 2019-12-11 02:59:30

问题


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.


回答1:


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");


来源:https://stackoverflow.com/questions/7042056/t4mvc-and-testing-with-mvccontrib-testhelpers-getting-the-relative-virtual-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!