我正在使用 Rhino Mocks 编写我的单元测试,并且我想使用Assert.WasCalled
功能,但我一直收到错误消息。
一堆测试使用的我的帮助方法:
Private Function CreateSecurityTicketHelper(userName As String, validFrom As DateTime, validTo As DateTime) As ISecurityTicket
' Prepare a mock object for ITicketingDataManager interface
Dim dataManagerMock = MockRepository.GenerateMock(Of ITicketingDataManager)()
' Prepare a mock function for ITicketingDataManager.InitializeNewTicket(string, string)
Dim returnSecurityTicket As Func(Of String, String, ISecurityTicket) = Function(u, k) New SecurityTicketEntity() With {.UserName = u, .Key = k}
dataManagerMock.Stub(Function(x) x.InitializeNewTicket(Nothing, Nothing)).IgnoreArguments().Do(returnSecurityTicket)
' Create new TicketingManager instance
Dim ticketingManager = New TicketingManager(dataManagerMock)
' Try creating new security ticket
Dim ticket = ticketingManager.CreateSecurityTicket(userName, validFrom, validTo)
' Check if proper ITicketingDataManager method was invoked
dataManagerMock.AssertWasCalled(Sub(x) x.InitializeNewTicket(Nothing, Nothing), Sub(z) z.Repeat.Once())
' Return the ticket
Return ticketingManager.CreateSecurityTicket(userName, validFrom, validTo)
End Function
我可以调试该方法并且一切正常,直到AssertWasCalled
调用方法,当我得到以下异常时:
测试方法 Authentication.UnitTests.TicketingManagerTests.CreateSecurityTicket_ValidUserNameAndKey_TicketIsCreated 抛出异常:Rhino.Mocks.Exceptions.ExpectationViolationException: ITicketingDataManager.InitializeNewTicket(null, null); 预期 #1,实际 #0。