我有类似的测试代码:
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);
}
答案 0 :(得分:0)
区别在于Function(invocation) invocation.ReturnValue = 99
是一个返回Boolean
的内联函数,其中invocation => invocation.ReturnValue = 99
是一个内联函数,返回99
并将invocation.ReturnValue
设置为{ {1}}。
如果您使用的是足够晚的VB.NET版本,则可以使用99
,除非Sub(invocation) invocation.ReturnValue = 99
需要返回值。