I'm having a problem with WhenCalled not finding the correct override when a method parameter is a nullable type:
(Abbreviated sanitized example)
class CExternalDevice
{
// MR_1
internal CRawResponseData MakeRequest( RQHeader header, Byte[] cmdPayloadData, UInt32? expectedResponseSize)
{
// talk to external device not avail during test
// ...
// ultimately
return new CRawResponseData();
}
// MR_2
internal CRawResponseData MakeRequest( RQHeader header, UInt32? expectedResponseSize)
{
return WriteCommandPacket(header, new Byte[] {}, expectedResponseSize);
}
// MR_3
internal CRawResponseData MakeRequest( RQHeader header )
{
return WriteCommandPacket(header, new Byte[] {}, 0);
}
// MR_4
internal CRawResponseData MakeRequest( RQHeader header, Byte[] data)
{
return WriteCommandPacket( header, data, 0);
}
}
class CSomethingToTest
{
private CFancyResponseData SimpleMakeFancyRequest( CATEGORY category, CODE cmd )
{
RQHeader header = new RQHeader( category, code );
//Write packet with an unknown (null) response size
CRawResponseData response = _myExternalDevice.MakeRequest( header, expectedResponseSize: null );
return new CFancyResponseData( response );
}
private CRawResponseData SimpleMakeRequest( CATEGORY category, CODE cmd )
{
RQHeader header = new RQHeader( category, code );
CRawResponseData response = _myExternalDevice.MakeRequest( header );
return response;
}
}
[TestMethod( )]
public void MyTest()
{
CSomethingToTest testTarget = new CSomethingToTest();
bool fakeDeviceConnected = true;
CExternalDevice fakeExternalDevice = Isolate.Fake.Instance<CExternalDevice>( Members.ReturnRecursiveFakes, ConstructorWillBe.Ignored );
Isolate.WhenCalled( ( ) => fakeExternalDevice.Connected( ) ).DoInstead( c =>
{
return fakeDeviceConnected;
}
);
testTarget._myExternalDevice = fakeExternalDevice;
RQHeader dummyHeader = new RQHeader( 0, 0 );
Byte[ ] dummyData = new byte[ 0 ];
// UInt32? dummyResponseSize = null;
// doing this makes ANON_FAKE_MR_1 throw an exception "param cannot be null",
UInt32? dummyResponseSize = 0;
CRawResponseData simulatedResponse = new CRawResponseData();
Isolate.WhenCalled( ( ) => fakeExternalDevice.MakeRequest( dummyHeader ) )
.CallOriginal( );
Isolate.WhenCalled( ( ) => fakeExternalDevice.MakeRequest( dummyHeader, dummyResponseSize ) )
.CallOriginal( );
Isolate.WhenCalled( ( ) => fakeExternalDevice.MakeRequest( dummyHeader, dummyData ) )
.CallOriginal( );
// ANON_FAKE_MR_1
Isolate.WhenCalled( ( ) => fakeExternalDevice.MakeRequest( dummyHeader, dummyData, dummyResponseSize ) )
.DoInstead( callContext =>
{
return simulatedResponse;
}
);
////////////////
// this calls MR_3, and then ANON_FAKE_MR_1, as expected
testTarget.SimpleMakeRequest( 11, 22 );
/////////////////
// this calls MR_2, and then MR_1 (ignores ANON_FAKE_MR_1) NOT expected (or wanted)
testTarget.SimpleMakeFancyRequest( 10, 20 );
}
Any suggestions about what I'm doing wrong?