views:

1010

answers:

4

First of all, I am aware that this question is dangerously close to: http://stackoverflow.com/questions/1231860/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.

+1  A: 

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:

http://stackoverflow.com/questions/705833/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.

Lewis
+4  A: 

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")
    }
}


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

Granted, this is a very simple (so likely not the best) solution, but a start in the right direction.

Chris Missal
GetPath() should probably be virtual.
dahlbyk
A: 

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.

dahlbyk
+1  A: 

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.

Jeff
Integration tests like this are certainly useful to make sure all the pieces work together, but there are other scenarios (like a missing file) that this solution wouldn't be able to handle. Spawning the test web application is also a rather expensive operation, which can add up if you run tests frequently.
dahlbyk