A few of my controller actions call this method GenerateActionLink.
private string GenerateActionLink(string actionName, string token, string username)
{
string validationLink = null;
if (Request.Url != null)
{
var encodedToken = EncodedUrlParameter(token);
var url = Url.Action(actionName, "Register", new {Token = encodedToken, Username = username}, Request.Url.Scheme);
validationLink = url;
}
return validationLink;
}
This is to generate a validation link which will be sent via email. It works perfectly. The issue I'm having is when testing a controller action which called the method. Url is null.
How can i mock that and use the mock in my unit test ?
I am using Moq and NUnit.
NOTE: I need to be able to pass in an actionName, this is why i am not concatinating Request.Url with my parameter values because Request.Url will always have the action the request comes from which is generally not the action i want in the link, this is why i am using Url.Action to generate an action link.
UrlHelperand set the controller'sUrlproperty when arranging the test. To mock the request mock the controller's http context and set the controller context.