VB.Net: Rhino Mocks and the AAA syntax
From Wiki
Contents |
Introduction
In Rhino Mocks 3.5 Ayende introduced the AAA (Arange, Act and Assert) syntax. This make things a bit more easy to understand. Here is how to do it using VB.Net.
Simple Mocking example
Rhino mocks 3.4
This is the old version, works with Rhino mocks 3.4.
- Imports NUnit.Framework
- Imports Rhino.Mocks
- Namespace AnalysisManagement
- <TestFixture()> _
- <Category("ModelServices")> _
- <Category("ModelServices.AnalysisManagement")> _
- Public Class TestDAOFill
- #Region " Private members "
- Private _Mocker As MockRepository
- Private _Factory As DataAccessLayer.AnalysisManagement.Crud.Factory.IDAOFactory
- Private _Fill As ModelServices.AnalysisManagement.Interfaces.IFill
- #End Region
- #Region " Setup "
- <SetUp()> _
- Public Sub Setup()
- _Mocker = New MockRepository()
- _Factory = _Mocker.DynamicMock(Of DataAccessLayer.AnalysisManagement.Crud.Factory.IDAOFactory)()
- _MspFactory = _Mocker.DynamicMock(Of DataAccessLayer.MSP.Crud.Factory.IDAOFactory)()
- _Fill = New ModelServices.AnalysisManagement.DAOFill(_Factory, _MspFactory)
- End Sub
- <TearDown()> _
- Public Sub TearDown()
- _Mocker.ReplayAll()
- _Mocker.VerifyAll()
- End Sub
- #End Region
- #Region " Tests "
- <Test()> _
- Public Sub FindAllFiberFluorescenceColorN21s()
- Dim _CrudIFiberFluorescenceColorN21 As DataAccessLayer.AnalysisManagement.Crud.Interfaces.IFiberFluorescenceColorN21 = _Mocker.DynamicMock(Of DataAccessLayer.AnalysisManagement.Crud.Interfaces.IFiberFluorescenceColorN21)()
- Expect.Call(_Factory.CrudFiberFluorescenceColorN21).IgnoreArguments.Return(_CrudIFiberFluorescenceColorN21)
- Expect.Call(_CrudIFiberFluorescenceColorN21.FindAll).IgnoreArguments.Return(New List(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21))
- _Mocker.ReplayAll()
- Dim _templist As IList(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21) = _Fill.FindAllFiberFluorescenceColorN21s
- Assert.IsNotNull(_templist)
- Assert.IsInstanceOfType(GetType(IList(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21)), _templist)
- End Sub
- #End Region
- End Class
- End Namespace
Ok, so we notice the use of _mocker and DynamicMock to create our Mock objects and then we set our expectations, then we do a replayall and execute the methods under test. When the test is done the verifyall is executed which looks if our expectations are met.
Rhino mocks 3.5
Now we switch to Rhino mocks 3.5 and see that we get an error on Expect saying that the signature is not correct. No worry this is because it is choosing the wrong Expect. It is namely trying to use the extension method there. Just add Rhino.Mocks. before the Expect and all is well again. Look how the imports doesn't do the same thing.
And now on to the new.
- Imports NUnit.Framework
- Imports Rhino.Mocks
- Namespace AnalysisManagement
- <TestFixture()> _
- <Category("ModelServices")> _
- <Category("ModelServices.AnalysisManagement")> _
- Public Class TestDAOFill
- #Region " Private members "
- Private _Factory As DataAccessLayer.AnalysisManagement.Crud.Factory.IDAOFactory
- Private _Fill As ModelServices.AnalysisManagement.Interfaces.IFill
- #End Region
- #Region " Setup "
- <SetUp()> _
- Public Sub Setup()
- _Factory = MockRepository.GenerateMock(Of DataAccessLayer.AnalysisManagement.Crud.Factory.IDAOFactory)()
- _MspFactory = MockRepository.GenerateMock(Of DataAccessLayer.MSP.Crud.Factory.IDAOFactory)()
- _Fill = New ModelServices.AnalysisManagement.DAOFill(_Factory, _MspFactory)
- End Sub
- <TearDown()> _
- Public Sub TearDown()
- End Sub
- #End Region
- #Region " Tests "
- <Test()> _
- Public Sub FindAllFiberFluorescenceColorN21s()
- Dim _CrudIFiberFluorescenceColorN21 As DataAccessLayer.AnalysisManagement.Crud.Interfaces.IFiberFluorescenceColorN21 = MockRepository.GenerateMock(Of DataAccessLayer.AnalysisManagement.Crud.Interfaces.IFiberFluorescenceColorN21)()
- _CrudIFiberFluorescenceColorN21.Stub(Function(e) e.FindAll()).Return(New List(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21))
- _Factory.Stub(Function(e) e.CrudFiberFluorescenceColorN21).Return(_CrudIFiberFluorescenceColorN21)
- Dim _templist As IList(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21) = _Fill.FindAllFiberFluorescenceColorN21s
- Assert.IsNotNull(_templist)
- Assert.IsInstanceOfType(GetType(IList(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21)), _templist)
- End Sub
- #End Region
- End Class
- End Namespace
Look at how the _mocker.DynamicMock are replaced by MockRepository.GenerateMock and how the _mocker object has disappeared all together. The Expect.Call's have been replaced with Object.Stub methods. And the ReplayAll and VerifyAll are now gone. Overall A bit simpler and more meaningfull.


