模拟索引属性的 Moles
我正在寻找一种使用 Moles 来“绕过”配置文件中的单个项目的方法。我可以这样做:
NameValueCollection appsettings = new NameValueCollection();
appSettings.Add("MyKey", "MyValue");
System.Configuration.Moles.MConfigurationManager.AppSettingsGet = () => this.appSettings;
这工作得很好,但是我试图测试的类使用了一些其他配置设置,包括 ConfigSections 和 Moles detour 似乎已经打破了这一点。我只想替换特定值,而不是整个部分。在TypeMock中,我可以这样做:
Isolate.WhenCalled(() => ConfigurationManager.AppSettings["MyKey"]).WithExactArguments().WillReturn("MyValue");
当我模拟configurationManager时使用 TypeMock,我的测试通过了,但是当我使用 Moles 版本(看起来它应该做同样的事情)时,它失败了,这意味着 Moles 必须以某种方式影响 ConfigurationManager 类TypeMock 没有。
任何关于如何使用鼹鼠以 TypeMock 的方式行事的帮助将不胜感激。
I am looking for a way to 'detour' a single item in a configuration file with Moles. I can do this:
NameValueCollection appsettings = new NameValueCollection();
appSettings.Add("MyKey", "MyValue");
System.Configuration.Moles.MConfigurationManager.AppSettingsGet = () => this.appSettings;
This works fine, however the class which I am trying to test uses some other configuration settings including ConfigSections and the Moles detour seems to have broken this. I only want to replace a specific value, not the whole section. In TypeMock, I could do this:
Isolate.WhenCalled(() => ConfigurationManager.AppSettings["MyKey"]).WithExactArguments().WillReturn("MyValue");
When I mock the configurationManager using TypeMock, my test passes, but when I use the Moles version (which looks like it should do the same thing) it fails meaning that Moles must be affecting the ConfigurationManager class in a way which TypeMock doesn't.
Any help on how I can use moles to just behave in the way which TypeMock does would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
复制当前的 app.settings 部分:
通过调用 Set/Add 函数修改您需要的部分(请记住,“Add”函数不会覆盖现有值,但另一方面,“Set”总是创建一个新条目并删除现有值):
创建委托:
make a copy of current app.settings section:
Modify the part you need by calling Set/Add function (keep in mind that 'Add' function does not overwrite existing values but it adds a new value for the key. On the other hand, 'Set' always creates a new entry and gets rid of the existing values):
Create the delegate: