为什么RhinoMocks在VB和VB中表现不同? C#?

时间:2013-07-11 11:18:03

标签: c# vb.net unit-testing rhino-mocks rhino-mocks-3.5

我有类似的测试代码:

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

此代码的行为并不像预期的那样。变量result包含Integer.MinValue,而不是99,正如预期的那样。

如果我在C#中编写等效代码,它按预期工作:result包含99。

任何想法为什么?

C#等价物:

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);
}

1 个答案:

答案 0 :(得分:0)

区别在于Function(invocation) invocation.ReturnValue = 99是一个返回Boolean的内联函数,其中invocation => invocation.ReturnValue = 99是一个内联函数,返回99并将invocation.ReturnValue设置为{ {1}}。

如果您使用的是足够晚的VB.NET版本,则可以使用99,除非Sub(invocation) invocation.ReturnValue = 99需要返回值。