0

Consider the following interface...

public interface MyInterface
{
    bool MyProperty { get; }
}

I am attempting to stub out a call to the get function of this property in fakeiteasy.

[Test]
public void Foo()
{
    var fake = A.Fake<MyInterface>();
    var sut = new MySUT(fake);
    A.CallTo(() => fake.MyProperty).Returns(true);

    var result = sut.Foo();

    Assert.IsTrue(result);
}

The system-under-test's Foo() method simply returns the value of the MyProperty get property call. Unfortunately, this test always fails. When debugging, it seems the get property is always returning false.

How can I stub out the return value of a get property call?

EDIT - Adding MySUT class's code (as requested in comments)

public class MySUT
{
    private readonly MyInterface _myInterface;

    public MySUT(MyInterface myInterface)
    {
        _myInterface = myInterface;
    }

    public bool Foo()
    {
        return _myInterface.MyProperty;
    }
}
Jesse Webb
  • 43,135
  • 27
  • 106
  • 143

1 Answers1

3

I changed

var sut = MySUT(fake);

to

var sut = new MySUT(fake);

and then it worked in my test solution.

I used FakeItEasy 1.7.4257.42 and NUnit for the test. The main project and test project was separated in 2 different assemblies.

Espen Burud
  • 1,871
  • 10
  • 9
  • 1
    The lack of `new` was actually just a typo when I was writting up the question for SO, my real code wasn't missing it. My real code was a bit more complicated so I didn't bother including it. Looking for the `new` keyword actually made me realize my other "not relavant" code was the problem. I had stubbed out a few more methods on `MyInterface` which were being executed in the constructor of `MySUT`. I stubbed these calls out *AFTER* the call to `new MySUT()` when they needed to be setup before the constructor call. Serves me right for trying to simplify. – Jesse Webb Jan 03 '12 at 15:04