2

我有类似这样的测试代码:

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.MinValue99。

如果我用 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);
}
4

1 回答 1

0

不同之Function(invocation) invocation.ReturnValue = 99处在于内联函数返回 a Boolean,而invocation => invocation.ReturnValue = 99内联函数返回99并设置invocation.ReturnValue99

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

于 2014-03-29T06:59:53.283 回答