Rhino Mocks: how to verify that a method is called exactly once using vb.net and AAA syntax

I am trying to use the AAA syntax in Rhino Mocks with VB.Net to verify that the method was called only once. I don't seem to understand. With this code, if the repository is called twice, it returns nothing in the second call, and the test passes. I expected the test to fail when calling VerifyAllExpectations.

<TestMethod()>
Public Sub GetDataCallsRepositoryOneTime()
    Dim repository As IDataRepository = MockRepository.GenerateMock(Of IDataRepository)()
    Dim cacheRepository As New CachingDataRepository(repository)
    Dim results1 As IEnumerable(Of DataItem)
    Dim results2 As IEnumerable(Of DataItem)

    'verify that the base repository was asked for its data one time only
    repository.Expect(Function(x) x.GetData(1)).Return(GetSampleData).Repeat.Once()

    results1 = cacheRepository.GetData(1)
    results2 = cacheRepository.GetData(1)

    sdr.VerifyAllExpectations()
End Sub
+5
source share
2 answers

If you use VS2010, you get significantly improved lamba support (including the much better experience using Rhino Mocks with VB)

, AAA w/rhino mocks ( #), ,

, ( , )

Public Class Class1
    Public Overridable Sub Happy()

    End Sub

    Public Overridable Sub DoIt()
        Me.Happy()
        Me.Happy()
    End Sub
End Class

, AAA + vb Happy, 2x

<TestClass()>
Public Class UnitTest2

    <TestMethod()>
    Public Sub TestMethod1()
        Dim x = MockRepository.GeneratePartialMock(Of Class1)()

        x.DoIt()

        x.AssertWasCalled(Sub(y) y.Happy(), Sub(z) z.Repeat.Times(2))
    End Sub

End Class
+4

AssertWasCalled VB, () -

Public Interface datarepository
    Function GetData(ByVal id As Integer) As IEnumerable(Of String)
End Interface

Public Class cacherepository
    Dim _datarepository As datarepository
    Dim results As New Dictionary(Of Integer, IEnumerable(Of String))

    Public Sub New(ByVal datarepository As datarepository)
        _datarepository = Datarepository
    End Sub

    Public Function GetData(ByVal id As Integer) As IEnumerable(Of String)
        Dim result As New List(Of String)
        If results.TryGetValue(id, result) = False Then
            result = _datarepository.GetData(id)
            'Uncomment the following line to make the test pass'
            'results.Add(id, result)'
        End If
        GetData = result
    End Function
End Class

<TestClass()>
Public Class UnitTest1

    <TestMethod()>
    Public Sub TestMethod1()
        Dim datarep As datarepository = MockRepository.GenerateMock(Of datarepository)()
        Dim cacherep = New cacherepository(datarep)
        cacherep.GetData(1)
        datarep.Expect(Function(x) x.GetData(1)).Throw(New ApplicationException("FAILED TEST"))
        Try
            cacherep.GetData(1)
        Catch ex As ApplicationException
            Assert.Fail("GetData is called more than once")
        End Try
    End Sub

End Class
-1

All Articles