views:

210

answers:

2

Hi, I am trying to create a unit test similar to how I would have done one in C# but am struggling with the lambdas in vb.

Bascially I am trying to mock a class and then create a stub and return. In C# I would have done something like;

MockedPersonRepository
     .Stub(x => x.Find(id))
     .Return(person)

But in visual basic I am trying to do a similar thing but can not work out the syntax

   MockedPersonRepository.Stub(Function... argh!!!

Any advice on how to reproduce the above would be greatly appreciated!

Kind regards, Dom

+3  A: 

One easy example I typically show (as I'm a VB developer also) is the below: (for some odd reason in VB you need to pull this out into another function that returns nothing)

  <TestMethod()> _
  Public Sub Should_Call_Into_Repository_For_GetAllUsers()
    Dim Repository As IUserRepository = MockRepository.GenerateStub(Of IUserRepository)()
    Dim Service As IUserService = New UserService(Repository)

    Service.GetAllUserCollection()

    Repository.AssertWasCalled(Function(x) Wrap_GetAllUserCollection(x))
  End Sub

Function Wrap_GetAllUserCollection(ByVal Repository As IUserRepository) As Object
    Repository.GetAllUserCollection()

    Return Nothing
  End Function

The above is for interaction based testing, the below might be closer to what you are looking for in your current example

Dim StubUserObject As New User(1, "9999", "jdoe", "John", "Doe", 1)

    UserService.Stub(Function(x) x.GetUserByID(1)).[Return](StubUserObject)
Toran Billups
A: 

Would something like this work?

MockedPersonRepository_
    .Stub(Function(x) x.Find(id))_
    .[Return](person)
Andrew Hare