如何在ServiceStack中测试IAppSettings?

时间:2015-02-17 12:46:58

标签: servicestack

在ServiceStack中有一个IAppSettings如下:

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

如何在代码中创建具有属性的AppSettings实例?

我想在代码中创建一个IAppSettings用于测试,而不是从配置文件中获取值,例如web.config。

2 个答案:

答案 0 :(得分:3)

您还可以使用像moq这样的模拟框架来隐藏IAppSettings接口的实现:

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

然后,在测试中,只需将appSettings.Object传递给任何需要IAppSettings的服务/组件。

答案 1 :(得分:1)

列出AppSettings Wiki个不同的IAppSettings个提供商,例如DictionarySettings允许您从.NET Dictionary<string,string>填充设置。

AppSettingsTests也有助于了解如何测试不同的AppSettings提供程序。