Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A.Fake<Stream>().Read(...) throwing InvalidOperationException

Tags:

c#

fakeiteasy

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

like image 755
Richard Lang Avatar asked Jul 14 '15 23:07

Richard Lang


People also ask

Why do we throw invalidoperationexception?

Typically, it is thrown when the state of an object cannot support the method call. Because the InvalidOperationException exception can be thrown in a wide variety of circumstances, it is important to read the exception message returned by the Message property.

What is invalidoperationexception in Java?

The InvalidOperationException exception is thrown in cases where the failure to invoke a method is caused by reasons other than invalid arguments. Because the InvalidOperationException exception can be thrown in a wide variety of circumstances, it is important to read the exception message returned by the Message property.

Why do I get an invalidoperationexception when I call withdraw?

From the console output, you should notice that the third iteration left the account with a balance of zero, therefore, calling Withdraw on the fourth iteration caused this exception. The InvalidOperationException exception is thrown in cases where the failure to invoke a method is caused by reasons other than invalid arguments.

How do you throw an exception in a class?

The process of generating and signaling the error is referred to as throwing an exception. This is done using the throw keyword followed by a new instance of a class derived from System. Exception. There are standard exception types provided by the .NET framework that you can use rather than creating a custom exception.


1 Answers

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.

like image 181
Blair Conrad Avatar answered Sep 20 '22 15:09

Blair Conrad