MVC 3 unit testing controller but GetVirtualPathData sends strange VirtualPathData results

I'm encountering a strange behaviour when unit testing controller. I'm using some extension methods for building Url.Action and all works well when runing code in production but the unit test fails. In fact my custom Url.Action helper is using the method :


the exact line is :

VirtualPathData localizedPathData = HtmlExtensions.GetVirtualPathData(newRouteTable, Url.RequestContext, routingValues);

string url = localizedPathData.VirtualPath.ToLower();

The problem is that while unit testing the localizedPathData.VirtualPath is an empty string instead the url for the route like in the production code. The routingValues contains three keys "area", "controller", "action" with related values to the controller/action that I would like to have a url generated to. What's strange is that localizedPathData.Route property contains the correct route ! So why the localizedPathData.VirtualPath is an empty string ?

I tried with GetVirtualPathForArea but the result is the same.

I'm mocking with MvcContrib TestControllerBuilder.

The sample code is here :

    // Arrange 
var loginInput = new LoginInput { EmailAddress = "", Password = "test", RememberMe = false };

new RouteConfigurator().RegisterRoutes(() => new ResumeAreaRegistration().RegisterArea(new AreaRegistrationContext("Resume", RouteTable.Routes)));

var testControllerBuilder = new TestControllerBuilder(); 
var loginController = testControllerBuilder.CreateController<LoginController>();
loginController.Url = new UrlHelper(new RequestContext(loginController.HttpContext, new RouteData()), RouteTable.Routes);

const string defaultUrl = "/Dashboard";
// act 

var result = loginController.Index(loginInput, null);

// assert 


Maybe I'm missing something ? Something I forgot about ?

Thank you in advance for your help.


I was looking with Reflector and found that the reason it isn't working is that internally the RouteCollection object calls the ApplyAppPathModifier method on HttpResponseBase. The fix would be to setup an expectation on the ApplyAppPathModifier method of the HttpResponseBase mock to just return the value that is passed into it. If you are using Rhino Mocks, then it would look like this:

loginController.HttpContext.Response.Stub(x => x.ApplyAppPathModifier(Arg<string>.Is.Anything)).IgnoreArguments().Do((Func<string, string>)((arg) => { return arg; }));

Need Your Help

Referencing shell32 again, C# Visual Studio

c# visual-studio-2008 reference shell32

Hmmm. Okay after revisiting PInvoke, I'm sure that I don't quite get it :-/ (just asked this question)

JAVA (AES/CBC/PKCS5PADDING) What is the corresponding C/C++

c++ c aes crypto++

Cipher.getInstance("AES/CBC/PKCS5PADDING") i using the Sun JCE provider´╝Ü

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.