C#: Run each unit test with different configuration file

后端 未结 2 2127
醉酒成梦
醉酒成梦 2021-01-23 00:05

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.

相关标签:
2条回答
  • 2021-01-23 00:42

    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");
    
    0 讨论(0)
  • 2021-01-23 00:47

    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.

    0 讨论(0)
提交回复
热议问题