Question

I have a MVC controller that loads a resource file and uses Server.MapPath to get the path to the file. I want to mock out the Server property in the controller object using Microsoft Fakes framework (I know how to do this using other frameworks).

Here's the code:

    [HttpGet]
    public ActionResult GeneratePdf(string reportId)
    {
        var template = LoadTemplate(reportId);
        var document = pdfWriter.Write(GetReportModel(reportId), template);
        return File(document, MediaTypeNames.Application.Pdf);
    }

    private byte[] LoadTemplate(string reportId)
    {
        var templatePath = Server.MapPath(string.Format("~/ReportTemplates/{0}.docx", reportId));
        using(var templateContent = System.IO.File.OpenText(templatePath))
        {
            return Encoding.Default.GetBytes(templateContent.ReadToEnd());
        }
    }

The part I'm trying to mock out is the "Server.MapPath" method.

Was it helpful?

Solution

As of Visual Studio 2012 Update 1, you can detour the Controller.Server property using Stubs.

With the following .Fakes file in your test project:

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic="true">
  <Assembly Name="System.Web" Version="4.0.0.0"/>
  <StubGeneration>
    <Clear/>
    <Add FullName="System.Web.HttpContextBase!"/>
    <Add FullName="System.Web.HttpServerUtilityBase!"/>
  </StubGeneration>
  <ShimGeneration>
    <Clear/>
  </ShimGeneration>
</Fakes>

You can write the a unit test like this:

[TestMethod]
public void TestMethod1()
{
    var target = new TestController();

    var serverStub = new StubHttpServerUtilityBase();
    serverStub.MapPathString = (path) => path.Replace("~", string.Empty).Replace("/", @"\");

    var contextStub = new StubHttpContextBase();
    contextStub.ServerGet = () => serverStub;

    target.ControllerContext = new ControllerContext();
    target.ControllerContext.HttpContext = contextStub;

    var result = (FilePathResult) target.Index();
    Assert.AreEqual(@"\Content\Test.txt", result.FileName);
}

With the upcoming Update 2, you will also be able to detour Controller.Server property directly using Shims. Here is the additional .Fakes file you will need with this approach.

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic="true">
  <Assembly Name="System.Web.Mvc" Version="4.0.0.0"/>
  <StubGeneration>
    <Clear/>
  </StubGeneration>
  <ShimGeneration>
    <Clear/>
    <Add FullName="System.Web.Mvc.Controller!"/>
  </ShimGeneration>
</Fakes>

And here is the test:

[TestMethod]
public void TestMethod2()
{
    using (ShimsContext.Create())
    {
        var target = new TestController();

        var serverStub = new StubHttpServerUtilityBase();
        serverStub.MapPathString = (path) => path.Replace("~", string.Empty).Replace("/", @"\");

        var controllerShim = new ShimController(target);
        controllerShim.ServerGet = () => serverStub;

        var result = (FilePathResult)target.Index();
        Assert.AreEqual(@"\Content\Test.txt", result.FileName);
    }
}

Please note that this approach does not work in the current version (Update 1), due to limitations in Fakes runtime related to assemblies, like System.Web.Mvc that allow partially trusted callers. If you try to run the second test today, you will get a VerificationException.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top