2

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,

Complexity
  • 5,682
  • 6
  • 41
  • 84
  • [There's this approach](http://stackoverflow.com/a/6151688/501556) if you're interested in changing the default `app.config` at runtime. – Sameer Singh Nov 14 '13 at 06:44
  • 1
    your can use Microsoft enterprise library configuration block manager your configs.The configuration block is very easy to test – Kain Nov 14 '13 at 06: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. – Complexity Nov 14 '13 at 07:23

2 Answers2

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");
ozczecho
  • 8,649
  • 8
  • 36
  • 42
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.

TGH
  • 38,769
  • 12
  • 102
  • 135
  • 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, – Complexity Nov 14 '13 at 06:50
  • Please note that I'm using the ConfigurationManager, so it's this object that should be mocked than? – Complexity Nov 14 '13 at 07:20
  • 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. – TGH Nov 14 '13 at 07:23
  • 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. – Complexity Nov 14 '13 at 07:38