1

I'm currently developing a plugin to use in any application, and the configuration of the plugin is done through the usage of either a web.config or an app.config file.

Now, I want to unit test my plugin. I want to test the different behaviour in various conditions:

For exmaple:

  1. Only 1 single item should be marked as 'default'.
  2. A collection of providers can be registered in the plugin, however the name must be unique, otherwise, a specific exception is thrown.

My question:

I have a unit test project and I want to test each method with another application configuration file, is this possible?

I've tried reading the file from ExeConfiguration, but I don't get it to work.

Thanks for the feedback.

Kr,

3
  • There's this approach if you're interested in changing the default app.config at runtime. Commented Nov 14, 2013 at 6:44
  • 1
    your can use Microsoft enterprise library configuration block manager your configs.The configuration block is very easy to test Commented Nov 14, 2013 at 6:48
  • Hello, Changing the AppSettings will not work since I'm really relying on custom configuration sections. I would like to have different app.config in my unit test, and let each unit test select a different one. Commented Nov 14, 2013 at 7:23

2 Answers 2

3

This is what we do (same as @THG) explained in his answer.

public interface IConfiguration
{
    string SettingA { get; }


    string SettingB { get; }
}


public class Configuration : IConfiguration
{

    public string SettingA
    {
        get
        {
            return ConfigurationManager.AppSettings["SettingA"];
        }
    }

    public string SettingB
    {
        get
        {
            return ConfigurationManager.AppSettings["SettingB"];
        }
    }
}

Then in your test

    var config = MockRepository.GenerateStub<IConfiguration>();
    config.Stub(x => x.SettingA).Return("Fred");
Sign up to request clarification or add additional context in comments.

Comments

2

My recommendation is to mock the integration with the config file instead of using it directly. This approach offers much more flexibility and removes the need to worry about multiple config files. You can either use a mocking framework or create you own layer of abstraction on top of the code that fetches the config values.

4 Comments

Thanks for you very quick answer. Can you give me an example of how to do this with a mocking framework because I'm new to mocking frameworks. Kr,
Please note that I'm using the ConfigurationManager, so it's this object that should be mocked than?
You might find it easier to wrap the calls to ConfigurationManger and mock the wrapper. As I recall ConfigurationManger is a static class which makes it harder to mock directly.
Thanks again for your feedback, but could you give me an example on how I can achieve this. So each test should have a custom configuration section which varies from the one used in the previous test. Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.