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 7944339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:31:22+00:00 2026-06-04T00:31:22+00:00

I searched a lot and still couldn’t find a solid solution for this. Suppose

  • 0

I searched a lot and still couldn’t find a solid solution for this. Suppose you have methods in your application. This methods use “System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration” to access some setting in the web.config. If you try to test these methods, your tests will fail because your test project doesn’t have web.config.

What is the best way to solve this problem. For projects with simple config file, I usually use a method like this as facade method.

public class Config
{
    public static String getKeyValue(String keyName)
    {
        if (keyName == String.Empty) return String.Empty;
        String result = "";

        System.Configuration.Configuration rootWebConfig1 =
           System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
        if (rootWebConfig1.AppSettings.Settings.Count > 0)
        {
            System.Configuration.KeyValueConfigurationElement reportEngineKey =
                rootWebConfig1.AppSettings.Settings[keyName];

            if (reportEngineKey != null)
            {
                result = reportEngineKey.Value;

            }

        }

        return result;
    }
}

Every time I tried to set the path for OpenWebConfiguration( ), I got the error “The relative virtual path is not allowed”

  • 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-06-04T00:31:25+00:00Added an answer on June 4, 2026 at 12:31 am

    To make that scenario more testable, I usually take the approach of making a “settings manager” of my own, and giving it an interface. So for example:

    public interface IConfig
    {
        string GetSettingValue(string settingName);
    }
    

    Then I can have my “real” implementation:

    public sealed class Config : IConfig
    {
        public string GetSettingValue(string settingName)
        {
            // your code from your getKeyValue() method would go here
        }
    }
    

    Then my code that uses it would take in an instance of this (this is an example of the Dependency Inversion Principal):

    public void DoStuff(IConfig configuration)
    {
        string someSetting = configuration.GetSettingValue("ThatThingINeed");
        // use setting...
    }
    

    So now for my production code, I can call DoStuff and pass in an instance of Config.
    When I need to test, I can use a mocking tool (Moq, JustMock, RhinoMocks, etc) to create a fake IConfig that returns a known value without hitting the actual .config file, or you can do it without a mocking framework by making your own mocks (and store them in your test project).

    public class ConfigMock : IConfig
    {
        private Dictionary<string, string> settings;
    
        public void SetSettingValue(string settingName, string value)
        {
            settings[settingName] = value;
        }
    
        public string GetSettingValue(string settingName)
        {
            return settings[settingName];
        }
    }
    

    and

    [Test]
    public void SomeExampleTest()
    {
        var config = new ConfigMock();
        config.SetSettingValue("MySetting", "SomeValue");
    
        var underTest = new MyClass();
        underTest.DoStuff(config);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I searched for this a lot, but I couldn't find a solution for this
I've searched quite a lot, but I still don't have an answer for this.
I've binged a lot for this stuff, but couldn't find direct ANSWER, I've searched
I have searched a lot and tried much but I can not find the
Hy there I searched a lot on google, but couldn't find an answer. Maybe
I searched a lot for a solution to handle icon badging while the application
I am new to this and have searched a lot this week. I am
I searched a lot, but couldn't find anything.. I just want to ask if
Searched a lot about this problem but never found a answer, that solved it.
I searched a lot but could not find a way to dump table relations

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.