tags:

views:

549

answers:

2

Hi, we are writing tests for a COM library written in VB 6.0.The problem we are facing is that, we are unable to access events declared in VB( withevents). We get exception, "object does not support set of events". How can we overcome this problem?

+1  A: 

For example, we have a class in VB 6.0 called DatabaseCommand.

Option Explicit

Public Event SavedSuccessfully()

Public Sub Execute(ByVal vAge As Integer, ByVal vName As String, ByVal vAddress As String)

    RaiseEvent SavedSuccessfully

End Sub

Now, personclass

Private WithEvents dbCommand As DatabaseCommand

Public Sub Init(ByVal vDBCommand As DatabaseCommand)

    Set dbCommand = vDBCommand

End Sub

Private Sub dbCommand_SavedSuccessfully()
    'not implemented
End Sub

Now, when try to test this ( after compiling the vb project)

MockRepository repository = new MockRepository();

PersonLib.DatabaseCommand db = repository.DynamicMock<PersonLib.DatabaseCommand>();

PersonLib.PersonClass person = new PersonLib.PersonClass();

person.Init(db);  --- this line throws error - Object or class does not support the set of events
+3  A: 

Your mocking framework is the problem here. The mock object returned by this call:

repository.DynamicMock<PersonLib.DatabaseCommand>();

implements the DatabaseCommand class's interface, but does not mock its events. Therefore, when you pass an instance of this mock object to your VB6 code, which expects to receive a DatabaseCommand object that can raise events, it won't work.

When you pass the mock object to your PersonClass.Init method, here is simplified version of what is happening:

  1. The code gets to this line in PersonClass.Init:

    Set dbCommand = vDBCommand

  2. VB6 asks the object on the right-hand side of the Set statement if it supports the same events that the DatabaseCommand class does (VB6 does this because you declared dbCommand with the WithEvents keyword, so it will try to set up an event sink to receive events from the dbCommand object).

  3. The object you passed in, however, being a mock object and not a real DatabaseCommand object, doesn't actually implement the events that the real DatabaseCommand class implements. When VB6 encounters this, it raises the error you are seeing.

I can't think of a way to make the mock object support the same events that the DatabaseCommand class does in order make your test code work (well, I can think of one way, but it would involve redesigning your classes), but I may post more later if I find a more reasonable solution.

Mike Spross
Hi,If you could post some alternate solution, that will be great.