Question

I have test code similar to this:

Public Interface IDoSomething
   Function DoSomething(index As Integer) As Integer
End Interface

<Test()>
Public Sub ShouldDoSomething()
   Dim myMock As IDoSomething = MockRepository.GenerateMock(Of IDoSomething)()

   myMock.Stub(Function(d) d.DoSomething(Arg(Of Integer).Is.Anything))
      .WhenCalled(Function(invocation) invocation.ReturnValue = 99)
      .Return(Integer.MinValue)

   Dim result As Integer = myMock.DoSomething(808)

End Sub

This code doesn't behave as expected though. The variable result contains Integer.MinValue not 99, as expected.

If I write the equivalent code in C# it works as anticipated: result contains 99.

Any ideas why?

C# equivalent:

public interface IDoSomething
{
   int DoSomething(int index)
}

[test()]
public void ShouldDoSomething()
{
   var myMock = MockRepository.GenerateMock<IDoSomething>();

   myMock.Stub(d => d.DoSomething(Arg<int>.Is.Anything))
      .WhenCalled(invocation => invocation.ReturnValue = 99)
      .Return(int.MinValue);

   var result = myMock.DoSomething(808);
}
Was it helpful?

Solution

The difference will be Function(invocation) invocation.ReturnValue = 99 is an inline function returning a Boolean, where as invocation => invocation.ReturnValue = 99 is an inline function returning 99 and setting invocation.ReturnValue to 99.

If you're using a late enough version of VB.NET you can use Sub(invocation) invocation.ReturnValue = 99 unless WhenCalled expects a return value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top