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
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With