我如何使用 COM OLE 对象进行 TDD

发布于 2024-07-07 23:08:47 字数 739 浏览 9 评论 0原文

我有一个 OLE COM 对象,试图为其编写包装器,我决定开始使用 TDD 为其编写代码,因为我相信这会让我对要编写的内容有更好的方向感。 COM 对象有一个像这样的接口:

Interface Mapinfo
    Sub [Do](ByVal cmd As String)
    Function Eval(ByVal cmd As String) As String
End Interface

[Do] 命令将采用如下所示的内容

Mapinfo.Do("OpenTable("""C:\Temp\MyTable.TAB""")")

现在我正在尝试编写一个包装器,因此有一个像这样的函数:

Mapinfo.OpenTable("C:\Temp\MyTable.TAB")

现在我遇到的主要问题是,每次我想编写一个新的测试和一些代码,我必须创建 OLE 对象的实例,等待应用程序启动(30 秒以上),测试我的小功能,关闭并处置 OLE 对象,更改代码并再次运行它。

我的问题是:是否有更好的方法来完成所有这些操作,而不必每次都启动 OLE 应用程序? 我听说过模拟对象,但没有真正研究过它,他们会在这里帮助我吗? 如果是这样怎么办?

编辑:我现在意识到我必须为 Mapinfo 制作一个模拟对象,我的问题是如何制作一个可以采用不同格式字符串的模拟对象? 这将如何帮助我验证包装器中的代码是否正确?

I have an OLE COM object that trying to write a wrapper for, I have decided to start to use TDD to write the code for it as I believe that it will give me a better sense of direction with what I'm try to write. The COM object has a interface like this:

Interface Mapinfo
    Sub [Do](ByVal cmd As String)
    Function Eval(ByVal cmd As String) As String
End Interface

The [Do] command would take somthing like the following

Mapinfo.Do("OpenTable("""C:\Temp\MyTable.TAB""")")

Now I am trying to write a wrapper so there is a function like this:

Mapinfo.OpenTable("C:\Temp\MyTable.TAB")

Now my main problem that I'm having, is that every time I want to write a new test and some code I have to create an instance of the OLE object,wait for the application to start(30 secs+), test my little function,close and dispose OLE object, change code and run it all again.

My question is: Is there a better way to do all of this with out having to start the OLE app every time? I have heard about mock objects but haven't really looked into it much, would they help me here? If so how?

EDIT: I have now realized that I will have to make a mock object for Mapinfo, my question is how do I make a make a mock object that can take different formated strings? How will this help me verify that the code in my wrapper is correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心是晴朗的。 2024-07-14 23:08:47

是的,模拟对象会有所帮助。 本质上,您通过模拟 Mapinfo 接口来创建一个假的 Mapinfo 对象(顺便说一句,您应该重命名该 IMapInfo)。

然后,您指示该模拟需要调用什么以及返回什么结果(如果适用)。 您还可以创建测试,其中模拟会引发异常或执行其他难以使用真实对象调用的操作。

两个大型(且免费).NET 模拟框架是 MoQRhino 模拟。 Rhino 更加成熟,并且有更多配置模拟的方法。 MoQ 是新来者,与 Rhino 相比,其功能集较小,设定期望的方式也较少。

就我个人而言,我认为 MoQ 更适合新手嘲笑。 它相对容易理解,所有文档都与当前版本相关(搜索 Rhino 教程,你会得到几年前的垃圾,不再适用),而且它表现良好。

Yes, mock objects would help. Essentially, you create a fake Mapinfo object by mocking the Mapinfo interface (you should rename that IMapInfo, btw).

You then instruct that mock what calls to expect, and what results to return (if appropriate). You can also create tests where the mock throws exceptions or does other things that are hard to invoke using the real object.

The two big (and free) .NET mocking frameworks are MoQ and Rhino Mocks. Rhino is more mature and has more ways of configuring mocks. MoQ is the newcomer and has a smaller featureset and less ways of setting expectations than Rhino.

Personally, I think MoQ is better for the newcomer to mocking. Its relatively easy to understand, all the documentation out there is relevant to the current release (search for Rhino tutorials and you get junk from years ago that doesn't apply anymore), and it performs well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文