0

I am trying to do something similar to 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?

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
Ali
  • 1,396
  • 14
  • 37

1 Answers1

1

FakeItEasy can not be used to override SomeProperty. The problems is that Default is a member of type Settings which is a sealed class. In order to be able to use A.CallTo with Default.SomeProperty, Default must be a fake object created using A.Fake<…>.

Also, SomeProperty will need to be virtual or otherwise overridden, as shown in the documentation's What can be faked page.

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

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.

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
  • thanks for your reply. I think you're missing some code in your last line? – Ali Jul 21 '14 at 21:06
  • Thanks, @exalted. I'm easily distracted. Forgot the "modify" part of copy/paste/modify… – Blair Conrad Jul 21 '14 at 23:33
  • Thanks Blair, that makes a lot of sense! ;-) – Ali Jul 22 '14 at 07:05
  • Okay, that was too quick. Unfortunately `SomeProperty` doesn't have a setter. ;-( – Ali Jul 22 '14 at 08:16
  • Ah. I had never used project settings before I read your question. When I added them to a project I had lying around, I must have created a "User" property, which has a setter. Yours is an "Application" setting? In that case, if you can't just change the initial value (because you want to test with different values), I'd recommend adding a layer of abstraction. – Blair Conrad Jul 22 '14 at 11:02