Using FakeItEasy, how to get the value set on a property on a fake?
Asked Answered
I

1

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 ?????

Ireful answered 26/10, 2011 at 18:57 Comment(0)
H
12
var myObject = A.Fake<ISomeInterface>();

SomeMethod (myObject);
Assert.That (saved.MyProperty, Is.EqualTo(100));
Hannibal answered 27/10, 2011 at 20:8 Comment(4)
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. ;)Overrefinement
Well, there are two ways, either A.CallTo (() => myObject.MyProperty).Returns(100); or just myObject.MyProperty = 100;.Inadvertence
@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.Iraqi
@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.Inadvertence

© 2022 - 2024 — McMap. All rights reserved.