1

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。

4

1 に答える 1

2

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 has String parameters.

To accept any parameters in assertion:

dataManagerMock.AssertWasCalled(
    x => x.InitializeNewTicket(Arg<String>.Is.Anything, Arg<String>.Is.Anything),
    z => z.Repeat.Once());

To specify expected arguments (e.g. expected1, expected2):

dataManagerMock.AssertWasCalled(
    x => x.InitializeNewTicket(Arg<String>.Is.Equal(expected1), Arg<String>.Is.Equal(expected2)),
    z => z.Repeat.Once());

問題の理由を説明し、解決に役立つことを願っています:)。

于 2013-01-12T09:41:32.923 に答える