【发布时间】:2013-07-11 11:18:02
【问题描述】:
我有类似这样的测试代码:
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);
}
【问题讨论】:
-
您也应该提供 C# 代码。您认为等效的可能并非如此,并且可能是答案所在。
-
咦,为什么要提供两个不同的返回值?你不能放弃
WhenCalled()而只使用Return()吗?但不知道行为不同的原因。
标签: c# vb.net unit-testing rhino-mocks rhino-mocks-3.5