I have a MVC 4 controller that makes use of the System.Web.Mvc.HtmlHelper.GenerateLink method. I want to Unit Test the controller and therefore I have to mock this method. I'm using Moq.
My problem is that I get a NullReferenceException when invoking the GenerateLink method and I think this is because I have the first parameter (RequestContext) not mocked properly. But I don't know which properties of the RequestContext I have to mock and how to mock it to get rid of this error.
Currently I have this as setup for the mock:
var RequestContextMock = new Mock<RequestContext>();
RequestContextMock
.Setup(x => x.HttpContext.Request.ApplicationPath)
.Returns(@"/");
RequestContextMock
.Setup(x => x.HttpContext.Response.ApplyAppPathModifier(It.IsAny<string>()))
.Returns((string s) => s);
var Routes = new RouteCollection();
Routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional});
But that gives the NullReferenceException when the GenerateLink method is executed, so clearly I'm missing some values somewhere.
EDIT: The relevant part of the controller under test is:
public class _AccountInfoController : Controller
{
readonly IUserRepository _UserRepository;
readonly IIdentityRepository _IdentityRepository;
public _AccountInfoController(IUserRepository userRepository,IIdentityRepository identityRepository)
{
this._UserRepository = userRepository;
this._IdentityRepository = identityRepository;
}
[AllowAnonymous]
public ActionResult Index(RequestContext requestContext, RouteCollection routes)
{
var AccountInfoModel = new AccountInfoModel();
string Email = _IdentityRepository.GetEmailAddress();
User User = _UserRepository.GetByEmail(Email);
if (User != null)
{
string Link = HtmlHelper.GenerateLink(requestContext,routes,"Logoff",null,"Index","Logoff",null,null);
The Unit Test is:
[TestMethod]
public void HaveTenantNameInModel()
{
const string TenantName = "tenant name.";
Tenant TestTenant = new Tenant {Name = TenantName};
User TestUser = new User {CurrentTenant = (TestTenant)};
var UserRepository = new Mock<IUserRepository>();
UserRepository
.Setup(p => p.GetByEmail(null))
.Returns(TestUser);
AccountInfoModel AccountInfoModel = new AccountInfoModel();
var IdentityRepository = new Mock<IIdentityRepository>();
var ControllerUnderTest = new _AccountInfoController(UserRepository.Object,IdentityRepository.Object);
//Mock the request context
var RequestContextMock = new Mock<RequestContext>();
RequestContextMock
.Setup(x => x.HttpContext.Request.ApplicationPath)
.Returns(@"/");
RequestContextMock
.Setup(x => x.HttpContext.Response.ApplyAppPathModifier(It.IsAny<string>()))
.Returns((string s) => s);
var Routes = new RouteCollection();
Routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional});
dynamic Result = ControllerUnderTest.Index(RequestContextMock.Object, Routes);
Assert.IsTrue(Result.GetType() == typeof(PartialViewResult));
Assert.AreEqual(TenantName, Result.Model.TenantName);
}