I’m using Rhino Mocks to write my Unit Tests and I’d like to use Assert.WasCalled functionality, but I’m keep getting an error.
My help method used by a bunch of tests:
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
I can debug that method and all goes right until AssertWasCalled method is called, when I’m getting following exception:
Test method
Authentication.UnitTests.TicketingManagerTests.CreateSecurityTicket_ValidUserNameAndKey_TicketIsCreated
threw exception:
Rhino.Mocks.Exceptions.ExpectationViolationException:
ITicketingDataManager.InitializeNewTicket(null, null); Expected #1,
Actual #0.
Your assertion says that
InitializeNewTicket()method should be called once with arguments(Nothing, Nothing).If this method is being called with some another arguments then assertion fails.
You have to rewrite assertion to either A) accept any arguments or B) specify correct arguments.
See examples below.
Few notes about examples:
1. Ufortunatelly I’m not good in VB syntax so providing examples in C#.
2. It is not mentioned in question which parameters type has method
InitializeNewTicket()so for example I assume it hasStringparameters.To accept any parameters in assertion:
To specify expected arguments (e.g.
expected1, expected2):Hope that explains the reason of your issue and helps to solve :).