title: How do you get unit tests to use routes in ASP.NET MVC?
body: <p>I am writing unit tests against my ASP.NET MVC application, in particular I am testing an HtmlHelper extension method that I wrote.  There is a line inside of the extension method:</p>
  
  <pre><code>var innerHtml = htmlHelper.ActionLink(text, action, controller, routeValues, null);
  </code></pre>
  
  <p>When I run this inside of my unit test, the href of the generated URL is blank regardless of the action or controller passed in.</p>
  
  <p>Here is my unit test:</p>
  
  <pre><code>var page = CreateProductDataPage(); //returns ProductDataPage object
  var htmlHelper = Http.CreateHtmlHelperWithMocks&lt;ProductDataPage&gt;(new ViewDataDictionary&lt;ProductDataPage&gt;(page), false);
  var result = htmlHelper.ProductListingBreadcrumb(true, null, null);
  </code></pre>
  
  <p>Here is the CreateHtmlHelperWithMocks method:</p>
  
  <pre><code>public static HtmlHelper&lt;T&gt; CreateHtmlHelperWithMocks&lt;T&gt;(ViewDataDictionary&lt;T&gt; viewData, bool isLoggedIn) where T : class
  {
      var mockViewDataContainer = new Mock&lt;IViewDataContainer&gt;();
      mockViewDataContainer.SetupGet(v =&gt; v.ViewData).Returns(viewData);
  
      return new HtmlHelper&lt;T&gt;(GetViewContextMock(viewData, isLoggedIn).Object, mockViewDataContainer.Object);
  }
  </code></pre>
  
  <p>Finally, here is the GetViewContextMock method:</p>
  
  <pre><code>public static Mock&lt;ViewContext&gt; GetViewContextMock(ViewDataDictionary viewData, bool isLoggedIn)
  {
      var mock = new Mock&lt;ViewContext&gt;();
  
      mock.SetupGet(v =&gt; v.HttpContext).Returns(GetHttpContextMock(isLoggedIn).Object);
      mock.SetupGet(v =&gt; v.Controller).Returns(new Mock&lt;ControllerBase&gt;().Object);
      mock.SetupGet(v =&gt; v.View).Returns(new Mock&lt;IView&gt;().Object);
      mock.SetupGet(v =&gt; v.ViewData).Returns(viewData);
      mock.SetupGet(v =&gt; v.TempData).Returns(new TempDataDictionary());
      mock.SetupGet(v =&gt; v.RouteData).Returns(new RouteData());
  
      return mock;
  }
  </code></pre>