我可以使用模拟对象来模拟串行端口吗?

发布于 2024-11-02 14:07:35 字数 452 浏览 0 评论 0原文

我正在使用 C++/CLI 开发一个应用程序,该应用程序通过串行端口与设备进行通信。并使用 MS Test 对其进行测试。我只是在阅读有关模拟对象的内容,并且仅在概念上理解它。我在单元测试中面临许多挑战,主要是因为我用来通过串行端口向我的应用程序发送数据的模拟器以某种方式运行,而我无法让它改变其行为。

我想知道的是:

  • 我可以使用模拟对象来模仿串行端口行为吗?它的输入缓冲区?
  • 我可以使用模拟对象来更改串行端口行为(即假装端口被阻止)。
  • 模拟是否可以用于更基本的类(例如,我想测试分配数组的部分代码。我可以让模拟假装由于内存不足而导致内存分配失败)
  • Rhino Mocks 是否适合模拟 C++/ CLI 应用程序 ?
  • 根据您的经验,是否还有其他适合此任务的模拟框架(最好是免费软件,但对付费软件开放)?
  • 这些是否与 MS Test 兼容。

I am developing an application in C++/CLI that communicates with a device via a Serial Port. and testing them using MS Test. I'm just reading up about mock objects and have only understood it conceptually. I face many challeneges in unit testing, primarily because the simulator I use to send data over the serial port to my app behaves in a certain way and I cant get it to change its behaviour.

What I would like to know is:

  • Can I use mock objects to mimic the Serial Port behaviour? its input buffer?
  • Can I use the mock object to change the Serial Port behaviour(ie to pretend that the port is blocked).
  • Can mocks be used for more basic classes(For example I want to test a part of my code that allocates an array. Can I get the mock to pretend that memory allocation failed due to insufficient memory)
  • Is Rhino Mocks suitable for mocking a C++/CLI application ?
  • Any other mocking frameworks (prefereably freeware but open to paid ones) that in your experience is good for this task?
  • Are these comepatible with MS Test.

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

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

发布评论

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

评论(1

筑梦 2024-11-09 14:07:35

我是从 C 嵌入式背景来回答这个问题的,但我觉得你的问题在模拟方面相当普遍,答案应该适用。

我可以使用模拟对象来模仿串行端口行为吗?它的输入
缓冲区?

是的,这里有一个很好的模拟 UART 示例:
http ://throwtheswitch.org/white-papers/when-bad-code-runs-green.html

我可以使用模拟对象来更改串行端口行为(即假装端口>被阻止)。

这正是嘲笑的目的。只要你的代码被分成几层,比如

  • 硬件驱动层、
  • 控制层
  • 、外部模块的 API 层

,那么你就可以模拟模拟有效操作所需的任何一层:缓冲区已满、硬件故障等。

模拟可以用于更基本的类(例如我想测试我的代码的一部分>分配一个数组。我可以让模拟假装由于内存不足而导致内存分配失败)

是的,这类似于上一个问题是,如果您的代码已经包装了一些内存分配代码,那么您可以模拟包装器返回的内容。也许你有一个内存管理器类型类

Rhino Mocks 适合模拟 C++/CLI 应用程序吗?

对此不太确定

根据您的经验,还有其他适合此任务的模拟框架(最好是免费软件,但对付费软件开放)?

上面链接的示例来自 unity + CMock + Ceedling (主要是 C 测试框架)。它是开源的,非常适合嵌入式 C 应用程序。不确定 C++ 支持。

这些与 MS Test 兼容吗?

抱歉也不知道这个

I'm answering this from a C embedded background, but I feel like your questions are fairly general in regard to mocking, and the answers should apply.

Can I use mock objects to mimic the Serial Port behaviour? its input
buffer?

Yes here is a pretty good example of a mocked UART:
http://throwtheswitch.org/white-papers/when-bad-code-runs-green.html

Can I use the mock object to change the Serial Port behaviour(ie to pretend that the port >is blocked).

This is precisely what mocking is for. As long as your code is divided up into layers i.e. something like

  • Hardware driver layer
  • Control layer
  • API layer for outside modules

Then you could mock whichever layer necessary to simulate valid operation: buffer full, hardware failure, etc.

Can mocks be used for more basic classes(For example I want to test a part of my code > that allocates an array. Can I get the mock to pretend that memory allocation failed due to insufficient memory)

Yes, this is similar to the previous question in that if your code already wraps some memory allocation code then you can mock what is returned by the wrapper. Maybe you have a memory manager type class that

Is Rhino Mocks suitable for mocking a C++/CLI application ?

Not sure about this

Any other mocking frameworks (prefereably freeware but open to paid ones) that in your experience is good for this task?

The example linked above is from unity + CMock + Ceedling (mostly a C test framework). It's open source and has worked great for embedded C applications. Not sure on C++ support.

Are these comepatible with MS Test?

Sorry don't know this one either

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