anybody knows how to mock Url.Content("~") ?
(BTW: I’m using Moq)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You are referring to the
Urlproperty in the controllers, I presume, which is of typeUrlHelper. The only way we have been able to mock this is to extract anIUrlHelperinterface, and create aUrlHelperWrapperclass that both implements it and wraps the nativeUrlHelpertype. We then define a new property on ourBaseControllerlike so:This allows us to create mocks of
IUrlHelperand inject them, but in the default case to use an instance of ourUrlHelperWrapperclass. Sorry it’s long winded, but as you have discovered, it’s a problem otherwise. It does, however, drop in without the need to change any of yourUrl.Actionand similar calls in your controllers.Here’s the interface:
I won’t bother giving you the definition of
UrlHelperWrapper– it really is just a dumb wrapper that implements this, and passes all calls through toUrlHelper.