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?
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:
The code gets to this line in PersonClass.Init
:
Set dbCommand = vDBCommand
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With