3

I have a web project which reads multiple configuration keys from web.config. These settings are related to the rules implemented in the project. When the unit test invokes the model classes in the web project it ends up reading the app.config from the unit test project. So I have to replicate the keys in app.config in the unit test project. Is it better to move the configuration information to an external file in the web project to avoid the copies of the configuration information?

Thanks in advance.

Amol
  • 31
  • 1

2 Answers2

0

When testing web apps I end up doing the same as you, copying settings into the app.config file of the test project. There's nothing wrong with that.

The alternative would be to mock the config settings where they would be used, but this can be cumbersome, however it is still a valid approach. I tend to just keep it simple and create an app.config file.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
  • I am worried if a developer changes a setting in the actual web.config but forgets to modify app.config, it is possible for the unit test to continue to pass even though it is no longer testing the actual code. Is there any way to safeguard against such a possibility? – Amol Dec 29 '11 at 17:41
  • If you have someone on your team who does not communicate about making changes (i.e. to the web.config file) then you have bigger problems! As long as everyone is open about the changes they make, then there is nothing to worry about. – Jason Evans Dec 30 '11 at 10:52
0

I would use wrapper class which returns those settings. Example below

public interface ISettingStorage
{
    string GetSetting(string name);
}

public class SettingStorage : ISettingStorage
{
    public string GetSetting(string name)
    {
        // read the actual setting from the web.config
    }
}

This way you can mock the ISettingStorage in your unit tests to return whatever values you want. It will also slightly speed up the unit tests because there is no disk I/O.

Toni Parviainen
  • 2,217
  • 1
  • 16
  • 15