title: Testing WebApi Controller Url.Link
body: <p>I have the following controller action</p>
  
  <pre><code>public void Post(Dto model)
  {
      using (var message = new MailMessage())
      {
          var link = Url.Link("ConfirmAccount", new { model.Id });
  
          message.To.Add(model.ToAddress);
          message.IsBodyHtml = true;
          message.Body = string.Format(@"&lt;p&gt;Click &lt;a href=""{0}""&gt;here&lt;/a&gt; to complete your registration.&lt;p&gt;&lt;p&gt;You may also copy and paste this link into your browser.&lt;/p&gt;&lt;p&gt;{0}&lt;/p&gt;", link);
  
          MailClient.Send(message);
      }
  }
  </code></pre>
  
  <p>To test this I need to setup the controller context</p>
  
  <pre><code>var httpConfiguration = new HttpConfiguration(new HttpRouteCollection { { "ConfirmAccount", new HttpRoute() } });
  var httpRouteData = new HttpRouteData(httpConfiguration.Routes.First());
  var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://localhost");
  sut = new TheController
  {
      ControllerContext = new HttpControllerContext(httpConfiguration, httpRouteData, httpRequestMessage),
      MailClient = new SmtpClient { PickupDirectoryLocation = location }
  };
  </code></pre>
  
  <p>This seems like a lot of setup to test the creation of a link. Is there a cleaner way to do this? I have read about in-memory servers but that looks like it applies more to the httpclient than testing the controller directly.</p>