Unit testing the app.config file with NUnit

后端 未结 13 2138
夕颜
夕颜 2020-12-04 16:37

When you guys are unit testing an application that relies on values from an app.config file? How do you test that those values are read in correctly and how your program re

相关标签:
13条回答
  • 2020-12-04 17:03

    You can call the set method of ConfigurationManager.AppSettings to set the values required for that particular unit test.

    [SetUp]
    public void SetUp()
    {
      ConfigurationManager.AppSettings.Set("SettingKey" , "SettingValue");
      // rest of unit test code follows
    }
    

    When the unit test runs it will then use these values to run the code

    0 讨论(0)
  • 2020-12-04 17:04

    Actually, thinking on it further, I suppose what I should do is create a ConfigFileReader class for use in my project and then fake it out in the unit test harness?

    Is that the usual thing to do?

    0 讨论(0)
  • 2020-12-04 17:04

    I had the same issue,

    you can use Nunit-console.exe c:\path1\testdll1.dll c:\path2\testdll2.dll

    this works fine even though if both dlls point to different app.configs ex testdll1.dll.config and testdll2.dll.config

    if you want to use Nunit project config and wrap these two dlls then there is no way you can have two configs

    you have to have project1.config if your Nunit project is project1.nunit in the same location as Project1.nunit sits.

    hope this helps

    0 讨论(0)
  • 2020-12-04 17:07

    I usually isolate external dependencies like reading a config file in their own facade-class with very little functionality. In tests I can create a mock version of this class that implements and use that instead of the real config file. You can create your own mockup's or use a framework like moq or rhino mocks for this.

    That way you can easily try out your code with different configuration values without writing complex tests that first write xml-configuration files. The code that reads the configuration is usually so simple that it needs very little testing.

    0 讨论(0)
  • 2020-12-04 17:07

    A more elegant solution is to use plain old dependency injection on the configuration settings themselves. IMHO this is cleaner than having to mock a configuration reading class/wrapper etc.

    For example, say a class "Weather" requires a "ServiceUrl" in order to function (e.g. say it calls a web service to get the weather). Rather than having some line of code that actively goes to a configuration file to get that setting (whether that code be in the Weather class or a separate configuration reader that could be mocked as per some of the other responses), the Weather class can allow the setting to be injected, either via a parameter to the constructor, or possibly via a property setter. That way, the unit tests are extremely simple and direct, and don't even require mocking.

    The value of the setting can then be injected using an Inversion of Control (or Dependency Injection) container, so the consumers of the Weather class don't need to explicitly supply the value from somewhere, as it's handled by the container.

    0 讨论(0)
  • 2020-12-04 17:09

    I was facing similar problems with web.config.... I find an interesting solution. You can encapsulate configuration reading function, e.g. something like this:

    public class MyClass {
    
    public static Func<string, string> 
         GetConfigValue = s => ConfigurationManager.AppSettings[s];
    
    //...
    
    }
    

    And then normally use

    string connectionString = MyClass.GetConfigValue("myConfigValue");
    

    but in unit test initialize "override" the function like this:

    MyClass.GetConfigValue = s =>  s == "myConfigValue" ? "Hi", "string.Empty";
    

    More about it:

    http://rogeralsing.com/2009/05/07/the-simplest-form-of-configurable-dependency-injection/

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