I have just recently
started using Rhino Mocks for unit testing of our (that is, Kynetec (previously: GfK Animal and Crop Health / GfK Kynetec)'s) i-map(tm) application.
The thing is, I'm pretty new to VB.NET, and totally new to Rhino Mocks, so I'm having problems with even the basic things.
In the hope of helping others, here's the description of both: my problem and possible solutions.
We have an interface (IPersistenceService) that is responsible for getting file names from "open" and "save as" dialogs,
and then using registered readers and writers to actually read / write those files.
It also handles the list of files recently accessed by the user.
Next, we have an OpenRecentCommand (implementing ICommand) that is invoked
when the user selects one of the recently accessed files in the GUI.
It's Execute method is very simple - it should just call the OpenFile subroutine of the persistence service
with the appropriate file name passed as the parameter
(the file name is kept as a private field of the OpenRecentCommand instance).
All pretty easy, but how should this be coded using VB.NET and Rhino Mocks?
<TestMethod()>
Public Sub TestExecute()
Dim persistenceService As IPersistenceService =
MockRepository.GenerateMock(Of IPersistenceService)()
Dim command As New OpenRecentCommand("testFileName.tmp")
command.Execute(Nothing)
' Here, we want to check whether persistenceService.OpenFile was called
' with the "testFileName.tmp" file name...
End Sub
In Rhino Mocks, you'd use AssertWasCalled to check that there was a call to a routine.
The problem is, though, that most examples on the net are C# oriented,
and simple lambda expressions with subroutines and VB.NET (no problem using functions there)...
There are several ways to go about this, and here I show three of them;
all are equivalent, but all require that we add a new definition in the unit test class.
These definitions are listed first, and then the (abbreviated) unit test code.
Solution 1) - using AddressOf (which I find the simplest and easiest):
Private Sub OpenFileCall1(ByVal PersistenceService As IPersistenceService)
PersistenceService.OpenFile("testFileName.tmp")
End Sub
<TestMethod()>
Public Sub TestExecute()
Dim persistenceService As IPersistenceService =
MockRepository.GenerateMock(Of IPersistenceService)()
Dim command As New OpenRecentCommand("testFileName.tmp")
command.Execute(Nothing)
persistenceService.AssertWasCalled(AddressOf OpenFileCall1)
End Sub
Solution 2) - using a lambda expression with one parameter (if you really need to use lambda expressions):
Private Function OpenFileCall2(
ByVal PersistenceService As IPersistenceService) As Object
PersistenceService.OpenFile("testFileName.tmp")
Return Nothing
End Function
<TestMethod()>
Public Sub TestExecute()
. . .
persistenceService.AssertWasCalled(Function(x) OpenFileCall2(x))
End Sub
Solution 3) - using a lambda expression with two parameters (the most reusable one, I guess):
Private Function OpenFileCall3(
ByVal PersistenceService As IPersistenceService,
ByVal FileName As String) As Object
PersistenceService.OpenFile(FileName)
Return Nothing
End Function
<TestMethod()>
Public Sub TestExecute()
. . .
persistenceService.AssertWasCalled(Function(x) OpenFileCall3(x, "testFileName.tmp"))
End Sub
Anyway, when you see the code, it looks really easy.
It's finding the solution (especially for the first time) that's a bit harder... :-)
HTH
Update
VB.NET 2010 adds support for lambda expressions with Sub; and this makes the whole thing much easier:
<TestMethod()>
Public Sub TestExecute()
Dim persistenceService As IPersistenceService =
MockRepository.GenerateMock(Of IPersistenceService)()
Dim command As New OpenRecentCommand("testFileName.tmp")
command.Execute(Nothing)
persistenceService.AssertWasCalled(Sub(x) x.OpenFile("testFileName.tmp"))
End Sub
Top
|