【发布时间】:2013-12-16 09:28:06
【问题描述】:
我有一个无法测试的设置提供程序(企业遗留代码)。我正在尝试将设置提供程序包装在设置存储库中以减少不可测试代码的数量。因此,我们将不再有 20 个方法使用 Settings 提供程序,而是有 1 个。其余的实现 SettingsRepository 接口。
我在之后的测试中遇到了麻烦,这通常表明我做错了。
我希望你能帮助找出什么。
public class SettingsRepository : ISettingsRepository
{
public SettingsRepository() {Settings = Settings.Default; }
public Settings Settings { get; set; }
}
public interface ISettingsRepository
{
Settings Settings { get; set; }
}
我使用统一注入存储库。我从存储库中获取内容的方式如下:
_settingsRepository.Settings.SecureCache
所以这就是问题所在。我可以使用 nsubstitute 模拟/存根 SettingsRepository 接口,但实际上我需要做的是模拟“设置”以设置 SecureCache 的返回。
有没有办法在 nsubstitute 中“深度模拟”,所以我可以做类似的事情:
_settingsRepository.Settings.SecureCache.Returns("www.somepath.com");
目前“设置”为空,我没有可以在那里模拟的任何东西。
我的后备解决方案是直接在 SettingsRepository 上添加所有设置字段,但我希望避免这种情况,因为它只会将无法测试的代码移动到解决方案的其他位置。
【问题讨论】:
标签: c# unit-testing mocking stub nsubstitute