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?
我们正在为用VB 6.0编写的COM库编写测试。我们面临的问题是,我们无法访问VB中声明的事件(withevents)。我们得到例外,“对象不支持事件集”。我们怎样才能克服这个问题呢?
2 个解决方案
#1
3
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.
实现DatabaseCommand类的接口,但不模拟其事件。因此,当您将此模拟对象的实例传递给您希望接收可以引发事件的DatabaseCommand对象的VB6代码时,它将无法工作。
When you pass the mock object to your PersonClass.Init
method, here is simplified version of what is happening:
当您将模拟对象传递给PersonClass.Init方法时,这里是正在发生的事情的简化版本:
-
The code gets to this line in
PersonClass.Init
:代码到达PersonClass.Init中的这一行:
Set dbCommand = vDBCommand
设置dbCommand = vDBCommand
-
VB6 asks the object on the right-hand side of the
Set
statement if it supports the same events that theDatabaseCommand
class does (VB6 does this because you declareddbCommand
with theWithEvents
keyword, so it will try to set up an event sink to receive events from thedbCommand
object).如果VB6支持与DatabaseCommand类相同的事件,则VB6会询问Set语句右侧的对象(VB6这样做是因为您使用WithEvents关键字声明了dbCommand,因此它将尝试设置事件接收器以接收来自dbCommand对象的事件)。
-
The object you passed in, however, being a mock object and not a real
DatabaseCommand
object, doesn't actually implement the events that the realDatabaseCommand
class implements. When VB6 encounters this, it raises the error you are seeing.但是,传入的对象是模拟对象而不是真正的DatabaseCommand对象,实际上并不实现真正的DatabaseCommand类实现的事件。当VB6遇到这种情况时,会引发您看到的错误。
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.
我想不出一种方法可以让mock对象支持DatabaseCommand类所做的相同事件,以使你的测试代码工作(好吧,我可以想到一种方法,但它会涉及重新设计你的类),但我如果我找到更合理的解决方案,可以稍后发布。
#2
1
For example, we have a class in VB 6.0 called DatabaseCommand.
例如,我们在VB 6.0中有一个名为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)
现在,当试图测试这个(在编译vb项目之后)
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
#1
3
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.
实现DatabaseCommand类的接口,但不模拟其事件。因此,当您将此模拟对象的实例传递给您希望接收可以引发事件的DatabaseCommand对象的VB6代码时,它将无法工作。
When you pass the mock object to your PersonClass.Init
method, here is simplified version of what is happening:
当您将模拟对象传递给PersonClass.Init方法时,这里是正在发生的事情的简化版本:
-
The code gets to this line in
PersonClass.Init
:代码到达PersonClass.Init中的这一行:
Set dbCommand = vDBCommand
设置dbCommand = vDBCommand
-
VB6 asks the object on the right-hand side of the
Set
statement if it supports the same events that theDatabaseCommand
class does (VB6 does this because you declareddbCommand
with theWithEvents
keyword, so it will try to set up an event sink to receive events from thedbCommand
object).如果VB6支持与DatabaseCommand类相同的事件,则VB6会询问Set语句右侧的对象(VB6这样做是因为您使用WithEvents关键字声明了dbCommand,因此它将尝试设置事件接收器以接收来自dbCommand对象的事件)。
-
The object you passed in, however, being a mock object and not a real
DatabaseCommand
object, doesn't actually implement the events that the realDatabaseCommand
class implements. When VB6 encounters this, it raises the error you are seeing.但是,传入的对象是模拟对象而不是真正的DatabaseCommand对象,实际上并不实现真正的DatabaseCommand类实现的事件。当VB6遇到这种情况时,会引发您看到的错误。
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.
我想不出一种方法可以让mock对象支持DatabaseCommand类所做的相同事件,以使你的测试代码工作(好吧,我可以想到一种方法,但它会涉及重新设计你的类),但我如果我找到更合理的解决方案,可以稍后发布。
#2
1
For example, we have a class in VB 6.0 called DatabaseCommand.
例如,我们在VB 6.0中有一个名为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)
现在,当试图测试这个(在编译vb项目之后)
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