Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

Your profile

Search

July 2009
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

XML Feeds

Authors

« VB.Net: Visual studio intellisense doesn't like lambda expressionsReview of MCTS Self-Paced Training Kit (Exam 70-502): Microsoft .NET Framework 3.5 Windows Presentation Foundation »
The Desktop Developers Journal

VB.Net: Rhino Mocks and the AAA syntax

by chrissie1


Permalink 06 Oct 2008 04:44 , Categories: Microsoft Technologies Tags: ayende, rhino mocks, vb.net

So, last week Ayende made a new version of Rhino Mocks available, namely version 3.5. He made a post on what is new and shiny, but I would like to look at what the new AAA syntax has in store for us VB.Net users.

And I want to tell you how it used to look and how it looks now.

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.             <em>_Mocker.VerifyAll()</em>
  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.             <em>Expect.Call</em>(_Factory.CrudFiberFluorescenceColorN21).IgnoreArguments.Return(_CrudIFiberFluorescenceColorN21)
  37.             <em>Expect.Call</em>(_CrudIFiberFluorescenceColorN21.FindAll).IgnoreArguments.Return(New List(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21))
  38.             <em>_Mocker.ReplayAll()</em>
  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.

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 = <em>MockRepository.GenerateMock</em>(Of DataAccessLayer.AnalysisManagement.Crud.Factory.IDAOFactory)()
  19.             _MspFactory = <em>MockRepository.GenerateMock</em>(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 = <em>MockRepository.GenerateMock</em>(Of DataAccessLayer.AnalysisManagement.Crud.Interfaces.IFiberFluorescenceColorN21)()
  33.             _CrudIFiberFluorescenceColorN21.<em>Stub</em>(Function(e) e.FindAll()).Return(New List(Of Model.AnalysisManagement.Interfaces.IFiberFluorescenceColorN21))
  34.             _Factory.<em>Stub</em>(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 meaningful.

Here is another good article about the use of AAA and Rhino mocks but then in C#.

And it only goes to show that even the smartest and best people don’t get it right the first time ;D.

Ayende asked to add this to his wiki and so I did http://ayende.com/wiki/Rhino+mocks+3.5+and+VB.Net+the+AAA+syntax.ashx

Leave a comment »Send a trackback » 915 views

Trackback address for this post

Trackback URL (right click and copy shortcut/link location)

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)