14

Using FakeItEasy, I am trying to capture the setting of a property value on a fake object:

First the interface:

interface ISomeInterface
{
    int MyProperty {get;set;}
}

Then a fragment of unit test:

var myObject = A.Fake<ISomeInterface>();

int saved = 0;
A.CallTo (() => myObject.MyProperty).Invokes (x => saved = ?????);

SomeMethod (myObject);
Assert.That (saved, Is.EqualTo (100));

And having

void SomeMethod (ISomeInterface intf)
{
    intf.MyProperty = 100;
}

I don't know what to put to replace the ?????

Stécy
  • 11,951
  • 16
  • 64
  • 89

1 Answers1

12
var myObject = A.Fake<ISomeInterface>();

SomeMethod (myObject);
Assert.That (saved.MyProperty, Is.EqualTo(100));
Patrik Hägne
  • 16,751
  • 5
  • 52
  • 60
  • 1
    Regarding the second part of your answer, I want the fake object to actually return the value 100 so that other classes using this interface would use that value. I concede that my example was not clear enough. ;) – Stécy Oct 28 '11 at 12:25
  • 8
    Well, there are two ways, either A.CallTo (() => myObject.MyProperty).Returns(100); or just myObject.MyProperty = 100;. – Patrik Hägne Oct 28 '11 at 13:14
  • @Patrik Hägne, Second one where I assign the property directly is working for me. The first one A.CallTo(() => dbVM.CreateFolderPath).Returns(somePath); returned me this exception. The current proxy generator can not intercept the specified method for the following reason: - Non virtual methods can not be intercepted. – VivekDev Sep 08 '16 at 05:38
  • 1
    @VivekDev, I guess you're not faking an interface but a concrete class. Make sure to make the property virtual in the class you're faking. – Patrik Hägne Sep 08 '16 at 12:10