我可以使用模拟对象来模拟串口吗? [英] Can I use mock objects to mock a serial port?

查看:158
本文介绍了我可以使用模拟对象来模拟串口吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个通过串行端口与设备通信的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.

我想知道的是:


  • 我可以使用模拟物件以模拟串行端口行为?它的输入缓冲区?

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

  • mock用于更基本的类(例如我想测试我的代码的一部分,分配一个数组)我可以得到的模拟假设内存分配失败,由于内存不足)


  • 任何其他嘲笑框架(最好是免费软件,但开放给付费用户)在你的经验中对这个任务有好处吗?

  • 这些与MS Test相容。

  • 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.

推荐答案

从C嵌入式背景中回答这个问题,但我觉得你的问题在嘲笑方面是相当一般的,应该适用。

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?

是这里是一个很好的例子的嘲笑UART:
http:// throwtheswitch.org/white-papers/when-bad-code-runs-green.html

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).

这正是mocking的作用。只要您的代码分为多个层,例如

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


  • 硬件驱动程序层

  • 控制层

  • 外部模块的API层

然后你可以模拟任何模拟有效的层操作:缓冲区已满,硬件故障等。

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


可以将mock用于更基本的类(例如我想测试一部分我的代码>分配一个数组。我可以得到的模拟假设内存分配失败,由于内存不足)

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


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

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?

上面链接的例子是来自unity + CMock + Ceedling大多是C测试框架)。它是开源的,并且对嵌入式C应用程序非常有用。

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.





$ b

Are these comepatible with MS Test?


这些是否与MS Test兼容? b

很抱歉,不知道这个

Sorry don't know this one either

这篇关于我可以使用模拟对象来模拟串口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆