title: How to mock application path when unit testing Web App
body: <p>I am testing code in a MVC HTML helper that throws an error when trying to get the application path:</p>
  
  <pre><code>//appropriate code that uses System.IO.Path to get directory that results in:
  string path = "~\\Views\\directory\\subdirectory\\fileName.cshtml";
  htmlHelper.Partial(path, model, viewData); //exception thrown here
  </code></pre>
  
  <p>The exception that is thrown is</p>
  
  <blockquote>
    <p>System.Web.HttpException: The application relative virtual path '~/Views/directory/subdirectory/fileName.cshtml' cannot be made absolute, because the path to the application is not known.</p>
  </blockquote>
  
  <p>Following the advice of <a href="https://stackoverflow.com/questions/3425133/how-to-resolve-issue-with-image-path-when-testing-htmlhelper/3428418#3428418">How to resolve issue with image path when testing HtmlHelper?</a><br>
  I have faked (using Moq):</p>
  
  <ul>
  <li><code>Request.Url</code> to return a string</li>
  <li><code>Request.RawUrl</code> to return a string</li>
  <li><code>Request.ApplicationPath</code> to return a string</li>
  <li><code>Request.ServerVariables</code> to return a null NameValueCollection</li>
  <li><code>Response.ApplyAppPathModifier(string virtualPath)</code> to return a string</li>
  </ul>
  
  <p>What else is needed to be able to allow this code to run in the context of a unit test run?<br>
  Or<br>
  What other approach should I be taking to render a Partial view on a dynamically built string?</p>