C#: Run each unit test with different configuration file

后端 未结 2 2130
醉酒成梦
醉酒成梦 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();
        config.Stub(x => x.SettingA).Return("Fred");
    

提交回复
热议问题