Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

VB.Net: Rhino Mocks and the AAA syntax

From Wiki

Jump to: navigation, search

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.

  1. Imports NUnit.Framework
  2. Imports Rhino.Mocks
  3.  
  4. Namespace AnalysisManagement
  5.     <TestFixture()> _
  6.     <Category("ModelServices")> _
  7.     <Category("ModelServices.AnalysisManagement")> _
  8.     Public Class TestDAOFill
  9.  
  10. #Region " Private members "
  11.         Private _Mocker As MockRepository
  12.         Private _Factory As DataAccessLayer.AnalysisManagement.Crud.Factory.IDAOFactory
  13.         Private _Fill As ModelServices.AnalysisManagement.Interfaces.IFill
  14. #End Region
  15.  
  16. #Region " Setup "
  17.         <SetUp()> _
  18.         Public Sub Setup()
  19.             _Mocker = New MockRepository()
  20.             _Factory = _Mocker.DynamicMock(Of DataAccessLayer.AnalysisManagement.Crud.Factory.IDAOFactory)()
  21.             _MspFactory = _Mocker.DynamicMock(Of DataAccessLayer.MSP.Crud.Factory.IDAOFactory)()
  22.             _Fill = New ModelServices.AnalysisManagement.DAOFill(_Factory, _MspFactory)
  23.         End Sub
  24.  
  25.         <TearDown()> _
  26.         Public Sub TearDown()
  27.             _Mocker.ReplayAll()
  28.             _Mocker.VerifyAll()
  29.         End Sub
  30. #End Region
  31.  
  32. #Region " Tests "
  33. <Test()> _
  34.         Public Sub FindAllFiberFluorescenceColorN21s()
  35.             Dim _CrudIFiberFluorescenceColorN21 As DataAccessLayer.AnalysisManagement.Crud.Interfaces.IFiberFluorescenceColorN21 = _Mocker.DynamicMock(Of DataAccessLayer.AnalysisManagement.Crud.Interfaces.IFiberFluorescenceColorN21)()
  36.             Expect.Call(_Factory.CrudFiberFluorescenceColorN21).IgnoreArguments.Return(_CrudIFiberFluorescenceColorN21)
  37.             Expect.Call(_CrudIFiberFluorescenceColorN21.FindAll).IgnoreArguments.Return(New List(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21))
  38.             _Mocker.ReplayAll()
  39.             Dim _templist As IList(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21) = _Fill.FindAllFiberFluorescenceColorN21s
  40.             Assert.IsNotNull(_templist)
  41.             Assert.IsInstanceOfType(GetType(IList(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21)), _templist)
  42.         End Sub
  43. #End Region
  44.      End Class
  45. 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.

  1. Imports NUnit.Framework
  2. Imports Rhino.Mocks
  3.  
  4. Namespace AnalysisManagement
  5.     <TestFixture()> _
  6.     <Category("ModelServices")> _
  7.     <Category("ModelServices.AnalysisManagement")> _
  8.     Public Class TestDAOFill
  9.  
  10. #Region " Private members "
  11.         Private _Factory As DataAccessLayer.AnalysisManagement.Crud.Factory.IDAOFactory
  12.         Private _Fill As ModelServices.AnalysisManagement.Interfaces.IFill
  13. #End Region
  14.  
  15. #Region " Setup "
  16.         <SetUp()> _
  17.         Public Sub Setup()
  18.             _Factory = MockRepository.GenerateMock(Of DataAccessLayer.AnalysisManagement.Crud.Factory.IDAOFactory)()
  19.             _MspFactory = MockRepository.GenerateMock(Of DataAccessLayer.MSP.Crud.Factory.IDAOFactory)()
  20.             _Fill = New ModelServices.AnalysisManagement.DAOFill(_Factory, _MspFactory)
  21.         End Sub
  22.  
  23.         <TearDown()> _
  24.         Public Sub TearDown()
  25.      
  26.         End Sub
  27. #End Region
  28.  
  29. #Region " Tests "
  30. <Test()> _
  31.         Public Sub FindAllFiberFluorescenceColorN21s()
  32.             Dim _CrudIFiberFluorescenceColorN21 As DataAccessLayer.AnalysisManagement.Crud.Interfaces.IFiberFluorescenceColorN21 = MockRepository.GenerateMock(Of DataAccessLayer.AnalysisManagement.Crud.Interfaces.IFiberFluorescenceColorN21)()
  33.             _CrudIFiberFluorescenceColorN21.Stub(Function(e) e.FindAll()).Return(New List(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21))
  34.             _Factory.Stub(Function(e) e.CrudFiberFluorescenceColorN21).Return(_CrudIFiberFluorescenceColorN21)
  35.             Dim _templist As IList(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21) = _Fill.FindAllFiberFluorescenceColorN21s
  36.             Assert.IsNotNull(_templist)
  37.             Assert.IsInstanceOfType(GetType(IList(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21)), _templist)
  38.         End Sub
  39. #End Region
  40.       End Class
  41. 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.

597 Rating: 0.0/5 (0 votes cast)