简体   繁体   English

我可以使用FakeItEasy来伪造Properties.Settings.Default属性吗?

[英]Can I use FakeItEasy to fake Properties.Settings.Default properties?

I am trying to do something similar to A.CallTo(() => MyProject.Properties.Settings.Default.SomeProperty).Returns("Hello, World! ;-)"); 我正在尝试做类似于A.CallTo(() => MyProject.Properties.Settings.Default.SomeProperty).Returns("Hello, World! ;-)"); , but I do get… ,但是我得到了……

Non virtual methods can not be intercepted. 非虚拟方法不能被拦截。

… in return. … 作为回报。

Any ideas? 有任何想法吗?

FakeItEasy can not be used to override SomeProperty . FakeItEasy不能用于覆盖SomeProperty The problems is that Default is a member of type Settings which is a sealed class. 问题是Defaultsealed类型的Settings类型的成员。 In order to be able to use A.CallTo with Default.SomeProperty , Default must be a fake object created using A.Fake<…> . 为了能够将A.CallToDefault.SomeProperty一起使用, Default必须是使用A.Fake <…>创建的伪造对象。

Also, SomeProperty will need to be virtual or otherwise overridden, as shown in the documentation's What can be faked page. 此外,如文档的“可伪造的内容”页面所示, SomeProperty将需要是虚拟的或以其他方式覆盖。

If you need to be able to provide fake configuration in your tests, you could introduce a layer of abstraction around the configuration and fake that, with your production code using a concrete class that delegates to MyProject.Properties.Settings 如果您需要能够在测试中提供伪造的配置,则可以在配置周围引入一个抽象层,并通过使用委托给MyProject.Properties.Settings的具体类的生产代码伪造该抽象层。

An alternative (which I think is superior) is to avoid faking/mocking altogether and just change the settings directly, perhaps by doing something like this: 另一种选择(我认为是更好的选择)是避免完全伪造/模拟,而直接更改设置,也许可以这样做:

MyProject.Properties.Settings.Default.SomeProperty= "Hello, World! ;-)"

Although as pointed out in the comments, this is only an option if the property has a setter, which it seems that Application properties do not, but User properties do. 尽管如注释中所指出,但这仅是在属性具有setter的情况下的一种选择,似乎Application属性没有,而User属性却有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM