milo.farrell
milo.farrell

Reputation: 672

FakeItEasy - faking assignment

I have Some code where I have a fake configuration root. I would like to check that a call is made to set a config value.

var fakeConfigRoot = A.Fake<IConfigurationRoot>();

//Do something that will set config item

//A call to set the config must have happened

It is possible to fake getting a config item using

 A.CallTo(() => fakeConfigRoot["TestConfigItem"]).MustHaveHappened(Repeated.Exactly.Once);

I would like to know if it is possible using FakeItEasy to fake the assignment of a config item and if so how. If it is not possible can anyone think of any workarounds.

Upvotes: 3

Views: 476

Answers (1)

Blair Conrad
Blair Conrad

Reputation: 241790

You want to specify a call to a property setter using A.CallToSet. The examples don't show it, but the syntax works for an indexer as well. Try this:

var fakeConfigRoot = A.Fake<IConfigurationRoot>();

fakeConfigRoot["animal"] = "hippo";

A.CallToSet(() => fakeConfigRoot["animal"]).MustHaveHappened(Repeated.Exactly.Once);

Upvotes: 3

Related Questions