Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

Your profile

Search

November 2008
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

XML Feeds

Tags: rhino mocks

All the LessThanDot Journals

VB.Net: Rhino mocks 3.5 and Lambda expressions and AssertWasCalled not always working

by chrissie1


Permalink 07 Oct 2008 06:59 , Categories: Microsoft Technologies Tags: problem, rhino mocks, vb.net

There is no bug in Rhino mocks if that’s what you’re thinking. Rhino mocks 3.5 just uses things that VB.Net 9.0 can’t use and that can be frustrating. I wonder what possessed them to do it like that?

So let me tell the story.

I got a service and a repository, the service calls the repo.

Repo interface:

  1. Namespace Repository
  2.     Public Interface IRepository(Of T)
  3.         Function FindAll() As IList(Of T)
  4.         Sub Update(ByVal ObjectToUpdate As T)
  5.     End Interface
  6. End Namespace

The repo implementation:

  1. Imports System.Data.SqlClient
  2.  
  3. Namespace Repository
  4.     Public Class ProductRepository
  5.         Implements IRepository(Of Model.Product)
  6.  
  7.         ‘'’ <summary>
  8.         ‘'’ Here is where you would go to the database
  9.         ‘'’ </summary>
  10.         ‘'’ <returns>A list of Products</returns>
  11.         ‘'’ <remarks></remarks>
  12.         Public Function FindAll() As System.Collections.Generic.IList(Of Model.Product) Implements IRepository(Of Model.Product).FindAll
  13.             ‘Do something
  14.             Return Nothing
  15.         End Function
  16.  
  17.         Public Sub Update(ByVal ObjectToUpdate As Model.Product) Implements IRepository(Of Model.Product).Update
  18.             ‘Do something
  19.         End Sub
  20.     End Class
  21. End Namespace

The service interface:

  1. Namespace Service
  2.     Public Interface IService
  3.         Function FindAllProducts() As IList(Of Model.Product)
  4.         Sub Update(ByVal Product As Model.Product)
  5.     End Interface
  6. End Namespace

The service implementation:

  1. Namespace Service
  2.     Public Class Service
  3.         Implements IService
  4.  
  5.         Private _Repository As Repository.IRepository(Of Model.Product)
  6.  
  7.         Public Sub New(ByVal Repository As Repository.IRepository(Of Model.Product))
  8.             _Repository = Repository
  9.         End Sub
  10.  
  11.         Public Function FindAllProducts() As IList(Of Model.Product) Implements IService.FindAllProducts
  12.             Return _Repository.FindAll
  13.         End Function
  14.  
  15.         Public Sub Update(ByVal Product As Model.Product) Implements IService.Update
  16.             _Repository.Update(Product)
  17.         End Sub
  18.     End Class
  19. End Namespace

And now we want to test it.

First, we test the service’s findall method. We mock out the repo and see if the repo was called.

  1. <Test()> _
  2.         Public Sub Test_If_Service_Returns_A_List_Of_Products()
  3.             Dim _Repository As Repository.IRepository(Of Model.Product) = MockRepository.GenerateMock(Of Repository.IRepository(Of Model.Product))()
  4.             Dim _Service As Service.IService = New Service.Service(_Repository)
  5.             _Repository.Stub(Function(e) e.FindAll).IgnoreArguments.Return(New List(Of Model.Product))
  6.             Assert.IsNotNull(_Service.FindAllProducts)
  7.             _Repository.AssertWasCalled(Function(e) e.FindAll)
  8.         End Sub

This works just fine.

Now I want to see if the update method is also used as it should be, so I write this:

  1. <Test()> _
  2.         Public Sub Test_If_Service_Updates_A_Product()
  3.             Dim _Repository As Repository.IRepository(Of Model.Product) = MockRepository.GenerateMock(Of Repository.IRepository(Of Model.Product))()
  4.             Dim _Service As Service.IService = New Service.Service(_Repository)
  5.             _Service.Update(Nothing)
  6.             _Repository.AssertWasCalled(Function(e) e.Update(Nothing))
  7.         End Sub

But that gives me an error “Expression does not produce a value.". Mmmm, you can find an explanation for that here. And the solutions is there as well. The solution is, however, a bit clunky, as he says.

So in the end you could do this:

  1. <Test()> _
  2.         Public Sub Test_If_Service_Updates_A_Product()
  3.             Dim _Repository As Repository.IRepository(Of Model.Product) = MockRepository.GenerateMock(Of Repository.IRepository(Of Model.Product))()
  4.             Dim _Service As Service.IService = New Service.Service(_Repository)
  5.             _Service.Update(Nothing)
  6.             _Repository.AssertWasCalled(Function(e) wrap(e))
  7.         End Sub
  8.  
  9.         Function wrap(ByRef repo As Repository.IRepository(Of Model.Product)) As Object
  10.             repo.Update(Nothing)
  11.             Return Nothing
  12.         End Function

Which solve the problem, more or less.

Leave a comment »Send a trackback » 546 views

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 » 303 views

VB.Net: Check if an Event Gets Raised By a Method

by chrissie1


Permalink 29 Aug 2008 06:09 , Categories: Microsoft Technologies Tags: nunit, rhino mocks, unittest, vb.net

Let’s say we want to test to see if our object really throws that event when we need it to.

  1. Public Class EventThrower
  2.   Public Event throwme(byval somestring as String)
  3.  
  4.   Public Sub SomeSub()
  5.     RaiseEvent throwme("Yeepie!")
  6.   End Sub
  7. End Class

So when calling a method SomeSub an event throwme will be raised.

Now we make a little unittest class that looks like this.

  1. Imports NUnit.Framework
  2. Imports Rhino.Mocks
  3.  
  4. <TestFixture()> _
  5. Public Class TestEvent
  6.  
  7. #Region " Private members "
  8.         Private _Mocker As MockRepository
  9. #End Region
  10.  
  11. #Region " Setup and TearDown "
  12.         <Setup()> _
  13.         Public Sub Setup()
  14.             _Mocker = New MockRepository
  15.         End Sub
  16.  
  17.         <TearDown()> _
  18.         Public Sub TearDown()
  19.             _Mocker.ReplayAll()
  20.             _Mocker.VerifyAll()
  21.         End Sub
  22. #End Region
  23.  
  24. #Region " Tests "
  25.         <Test()> _
  26.         Public Sub Test_NewFile_If_RaisesEvent_NewFileMade()
  27.             Dim subscriber As ISubscriber = _Mocker.DynamicMock(Of ISubscriber)()
  28.             Dim _EventThrower As New EventThrower
  29.             AddHandler _EventThrower.throwme, AddressOf subscriber.throwmesub
  30.             subscriber.throwmesub(Nothing)
  31.             LastCall.IgnoreArguments()
  32.             _Mocker.ReplayAll()
  33.             _EventThrower.SomeSub()
  34.         End Sub
  35. #End Region
  36.  
  37.  
  38.         Public Interface ISubscriber
  39.             Sub throwmesub(ByVal somestring As String)
  40.         End Interface
  41.  
  42.     End Class
  43.  
  44.    
  45. End Namespace

Just a few notes. I created an interface just for this test. And interface with a method with the same signature as the event. Then in the Test, I subscribe the event to that method. I didn’t forget to set IgnoreArguments and I called the Method on our object.

If the test goes green, we know that the event is raised when we call the sub. Now it’s just a matter of subscribing to that event.

Leave a comment »Send a trackback » 275 views