A.Fake<Stream>().Read(...) throwing InvalidOperationException
Asked Answered
S

1

6

Using NUnit 2.6.4 & FakeItEasy 1.25.2 to unit test a C# code in Visual Studio 2013 Community Edition

The following test fragment executes as expected

[Test]
public void test_whatIsUpWithStreamRead()
{
    Stream fakeStream = A.Fake<Stream>();

    byte[] buffer = new byte[16];

    int numBytesRead = fakeStream.Read(buffer, 0, 16);

    Assert.AreEqual(0, numBytesRead);

}

however as soon as I decorate my fake with a CallTo/Returns() or ReturnsLazily() statement...

[Test]
public void test_whatIsUpWithStreamRead()
{
    Stream fakeStream = A.Fake<Stream>();

    A.CallTo(() => fakeStream.Read(A<byte[]>.Ignored, A<int>.Ignored, A<int>.Ignored)).Returns(1);

    byte[] buffer = new byte[16];

    int numBytesRead = fakeStream.Read(buffer, 0, 16);

    Assert.AreEqual(1, numBytesRead);

}

fakeStream.Read() throws a System.InvalidOperationException with the message:

"The number of values for out and ref parameters specified does not match the number of out and ref parameters in the call."

from within FakeItEasy.Configuration.BuildableCallRule.ApplyOutAndRefParametersValueProducer(IInterceptedFakeObjectCall fakeObjectCall), which seems pretty odd to me as Stream.Read() doesn't have any out/ref parameters.

Is this a bug that I should be reporting at https://github.com/FakeItEasy, or am I missing something?

thx

Sharleensharlene answered 14/7, 2015 at 23:36 Comment(1)
I'm not sure if you'd've seen the update, but we believe the problem to be resolved in FakeItEasy 1.25.3.Beware
B
2

Update: the bug has been fixed in FakeItEasy 1.25.3 and FakeItEasy 2.0.0.


Yes, it's a bug, which appears to have been introduced in 1.23.0. I've created issue 508. I'll work on a fix in the near future, and will discuss with the other project owners in what release we want to issue the fix. Head on over if you have an opinion.

In the meantime, one possible workaround is to roll back to FakeItEasy 1.22.0, if you don't require any of the enhancements and bug fixes that have been added in subsequent releases.

If that's not an option, perhaps consider abstracting away Stream.Read and faking the abstraction. Or come back and I'd be happy to discuss other paths.

Beware answered 15/7, 2015 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.