'How to test a IAppSettings in ServiceStack?

In ServiceStack there is an IAppSettings as follows:

var appSettings = new AppSettings();
DateTime lastUpdate = appSettings.Get<DateTime>("LastUpdated");
IList<string> allowedUsers = appSettings.GetList("AllowedUsers");
var redisConf = appSettings.Get<RedisConfig>("RedisConf");

How can I create an instance of AppSettings with properties in code?

I want to create a IAppSettings in code for testing and not to take the values from a configuration file, e.g., web.config.



Solution 1:[1]

You could also use a mocking framework like moq to stub out the implementation of the IAppSettings interface:

var appSettings = new Mock<IAppSettings>();
appSettings.Setup(s => s.Get<DateTime>("LastUpdated")).Returns(new DateTime(2015, 2, 1));

Then, in your test, just pass appSettings.Object to any service/component that needs an IAppSettings.

Solution 2:[2]

The different IAppSettings providers are listed AppSettings Docs, e.g. the DictionarySettings lets you populate the settings from a .NET Dictionary<string,string>.

The AppSettingsTests will also be helpful to see how to test different AppSettings providers.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PatrickSteele
Solution 2