Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Unit Testing: Testing a method that uses MapPath

First of all, I am aware that this question is dangerously close to: How to MapPath in a unit test in C#

I'm hoping however, that it has a different solution. My issue follows:

In my code I have an object that needs to be validated. I am creating unit tests for each validation method to make sure it is validating correctly. I am creating mock data and loading it into the object, then validating it. The problem is that within the validation, when an error occurs, an error code is assigned. This error code is used to gather information about the error from an xml file using Server.MapPath. However, when trying to get the xml file, an exception is thrown meaning the file cannot be found.

Since MapPath is in my validation code, and not my unit test, how do I get my unit test to recognize the path? Does this question make sense?

Error Line (In my Validation code NOT my unit test):

XDocument xdoc = XDocument.Load(HttpContext.Current.Server.MapPath("App_Data/ErrorCodes.xml"));

Simplified: The Unit Test calls a method in my program that calls Server.MapPath which then fails.

like image 923
Jeff Avatar asked Aug 11 '09 17:08

Jeff


4 Answers

I would abstract out the "filename provider" into an class that simply returns a location, then you can mock it much, much easier.

public class PathProvider
{
    public virtual string GetPath()
    {
        return HttpContext.Current.Server.MapPath("App_Data/ErrorCodes.xml");
    }
}

Then, you can either use the PathProvider class directly...

PathProvider pathProvider = new PathProvider();
XDocument xdoc = XDocument.Load(pathProvider.GetPath());

Or mock it out in your tests:

PathProvider pathProvider = new MockPathProvider(); // using a mocking framework
XDocument xdoc = XDocument.Load(pathProvider.GetPath());
like image 95
Chris Missal Avatar answered Oct 21 '22 15:10

Chris Missal


After some rigorous googling and some help from a colleague we came up with a simple solution already built into .net

Above the unit tests that accesses the validation process, I added:

 [TestMethod()]
 [HostType("ASP.NET")]
 [UrlToTest("http://localhost:###/upload_file.aspx")]
 [AspNetDevelopmentServerHost("Path To Web Application", "Path To Web Root")]

This works perfectly. Basically, when the test is called, it loads the URL with the specified unit test in the page load. Since it is a web site that is now calling the unit test, the validation will have access to Server.MapPath. This solution may not work for everyone, but it was perfect for this. Thanks to all you contributed.

like image 32
Jeff Avatar answered Oct 21 '22 14:10

Jeff


Try using Rhino Mocks or an alternative mocking framework to Mock the httpContext (or other dependent objects) Or you could write your own mock objects. Or write a MapPathWrapper class, inherit from a MapPathWrapperBase class for your real environment, then in for your unit tests create a MockMapPathWrapper object.

There should be plenty of examples for mocking on SO.

Here's one I asked:

How to use Rhino Mocks to Mock an HttpContext.Application

UPDATE I only have experience doing this with the Asp.Net MVC, with webforms I imagein it would be a lot more difficult because of the lack of an HttpContextBase class.

like image 43
Lewis Avatar answered Oct 21 '22 15:10

Lewis


I would extract methods that accept your dependencies as arguments:

public void Validate(HttpContext context)
{
    ValidatePath(context.Server.MapPath("App_Data/ErrorCodes.xml"));
}

public void ValidatePath(string path)
{
    XDocument xdoc = XDocument.Load(path);
    ValidateDocument(xdoc);
}

public void ValidateDocument(XDocument xdoc)
{
    // Original code
}

You can then test the various methods independently. For example, testing how ValidatePath() handles a missing file.

like image 43
dahlbyk Avatar answered Oct 21 '22 14:10

dahlbyk