Caves Travel Diving Graphics Mizar Texts Cuisine Lemkov Contact Map RSS Polski
Trybiks' Dive Texts Rhino Mocks Rhino Mocks's AssertWasCalled in VB.NET YAC Software
  Back

List

Charsets

Charts

DBExpress

Delphi

HTML

Intraweb

MSTest

PHP

Programming

R

Rhino Mocks

Software

Testing

UI Testing

VB.NET

VCL

WPF

Rhino Mocks's AssertWasCalled in VB.NET
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

Comments
#1
malik wrote on 2011-02-17 13:14:18
Thanks for the info! Spent quite some time trying to figure out why AssertWasCalled() does't work with a Sub in VB.NET 2009 and what to do about it...

Top

Add a comment (fields with an asterisk are required)
Name / nick *
Mail (will remain hidden) *
Your website
Comment (no tags) *
Enter the text displayed below *
 

Top

Tags

Rhino Mocks

Testing

VB.NET


Related pages

AssertWasCalled and Methods Called Multiple Times

AssertWas[Not]Called and Object Properties

Rhino Mocks's AssertWasNotCalled

Visual Studio - moving coded UI tests to a new / different project results in null reference exceptions

PrivateObject and Out/ByRef parameters

PrivateObject, WithEvents, and generics

Accessing private members of base classes

CA1800:DoNoCastUnnecessarily

PrivateObject and WithEvents

The creator of this fault did not specify a Reason.

Accessing private and protected members - PrivateObject and PrivateType

VS - Test Run Error - "COM object that has been separated from its underlying RCW cannot be used"

Saving / restoring window placements in .NET

Get the TreeViewItem for an Item in a WPF TreeView

Output in MSTest Tests (VS 2010)

Automated WPF tests and "Invalid URI: Invalid port specified."

Checking Property Change Notifications

Checking "Dangling" Event Handlers in Delphi Forms

First steps with Rhino Mocks (in VB.NET)

Meaningful identifiers

Public fields vs. properties

VS Pending Tests

Automated GUI Testing

Automated GUI Testing in VMs

Automated Testing of Window Forms

Detecting Memory Leaks with DUnit