3

I cannot understand why FakeItEasy does not allow me to set return value for public method with parameters.

The code:

var fakeInstanse = A.Fake<SomeClass>();
A.CallTo(() => fakeInstanse.Method(param1, param1));

The Method is the public one, accepts two parameters. Normally I would call Returns() method on second line of code, but Visual Studio does not show it among available.

What might affect this behavior? What part of SomeClass or Method definition might cause this?

shytikov
  • 9,155
  • 8
  • 56
  • 103

1 Answers1

2

In general, this should work. Check out this passing test:

public class SomeClass
{
    public virtual int Method(int arg1, int arg2)
    {
        return 7;
    }
}

[TestFixture]
public class TestFixture
{
    [Test]
    public void Should_be_able_to_set_return_value()
    {
        const int param1 = 9;
        var fakeInstanse = A.Fake<SomeClass>();
        A.CallTo(() => fakeInstanse.Method(param1, param1))
            .Returns(8);
        Assert.That(fakeInstanse.Method(param1, param1), Is.EqualTo(8));
    }
}

What's the return type of your Method? From your description, I'd guess that it's void.

Could you show us the declaration of SomeClass (and SomeClass.Method)? Otherwise, we're not going to be able to give constructive answers. Also, you may find some help at the FakeItEasy "what can be faked" documentation page.

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
  • I know this is old, but figured it's worth sharing. Make sure the object you are trying to return matches your method's return type. Sounds obvious, but when refactoring code (like I was) this had me confused for a good 15m. The compiler error wasn't too helpful even though it was clearly saying what the problem was in too many words. – dyslexicanaboko Sep 12 '22 at 19:35