mercredi 30 mars 2016

How to mock configuration for C# application

I wrote a C# application and would like to mock the configuration values. For example, I configure a path for file creation on remote machine and would like to create it locally when testing.

I thought of creating a wrapper class for the configuration that implements and interface, using this methodology one may mock the interface and return mocked values.

public interface IConfigurationProvider
{
   string GetFileOutputPath(string path);
}

public class ConfigurationWrapper : IConfigurationProvider
{
    return ConfigurationManager.AppSettings.Get("FileOutputPath");
}

Configuration:

<configuration>
    <appSettings>
        <add key="FileOutputPath" value="RemoteServerPath" />
    </appSettings>
</configuration>

This approach sure does the work, but is it best practice for this scenario?

Aucun commentaire:

Enregistrer un commentaire