Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 305523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:22:54+00:00 2026-05-12T07:22:54+00:00

First of all, I am aware that this question is dangerously close to: How

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:22:54+00:00Added an answer on May 12, 2026 at 7:22 am

    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());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, I am aware that this question has been discussed many times
First of all; I'm aware that this question is strange and I will probably
First of all, I'm completely aware that doing this will get my app rejected
I'm aware that this question has appeared in various forms before, but none of
First of all I'm aware that my question is same as in here .
First of all there is probably a question like this already but i couldn't
First of all - I apologize if this question shows ignorance or I'm not
First of all, I am sorry if this question doesn't belong to SO since
First of all, I am well aware of that there are many of questions
Ok guys I am completely aware this question has been asked on several sites

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.