如何创建使用起订量库的SerialPort模拟? [英] How can I create a SerialPort mock using the Moq library?

查看:219
本文介绍了如何创建使用起订量库的SerialPort模拟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了很多code与串行端口处理。一般会有连接在电线的另一端​​的装置和我通常创建自己嘲笑模拟它们的行为。

I have to write a lot of code that deals with serial ports. Usually there will be a device connected at the other end of the wire and I usually create my own mocks to simulate their behavior.

我开始看起订量来帮助我的单元测试。这是pretty的简单的使用它时,你需要的只是一个存根,但我想知道是否有可能,如果是我如何创建一个模拟的响应不同,根据我所要测试的硬件设备。

I'm starting to look at Moq to help with my unit tests. It's pretty simple to use it when you need just a stub, but I want to know if it is possible and if yes how do I create a mock for a hardware device that responds differently according to what I want to test.

一个简单的例子:

一设备I界面与接收命令(移动到位置x),给出了回ACK消息,并进入一个移动的状态,直到它到达位置有序

One of the devices I interface with receives a command (move to position x), gives back an ACK message and goes to a "moving" state until it reaches the ordered position.

我想创建一个测试,我送移动命令,然后不断查询状态,直到它到达最后的位置。

I want to create a test where I send the move command and then keep querying state until it reaches the final position.

我要创建两个版本的模拟了两种不同的测试,之一,我希望该设备顺利到达最终的位置,并在那里将失败其他。

I want to create two versions of the mock for two different tests, one where I expect the device to reach the final position successfully and the other where it will fail.

过分的要求?

推荐答案

如果您有程序中的串行端口对象的接口,然后起订量可以做到这一点通过创建一个模拟串行端口对象(MOQ好为存根和任何复杂的嘲弄)。

If you have an interface for a serial port object in your program, then Moq can do that by creating a mock serial port object (Moq is good for both stubs and mocks of any complexity).

如果你想的拦截的调用,以首创置业的SerialPort 类方可达到硬件(而无需创建一个串口接口和实现,再加上测试实施),那么你就需要更强大的东西。这就是是。

If you'd like to intercept the calls to the BCL SerialPort class before they reach the hardware (without having to create a serial port interface and implementation, plus a test implementation), then you need something more powerful. That is what Moles is for.

如果你想真正模拟装置,那么这种超越单元测试。在这个层面上,它可以使用 com0com 添加一对虚拟串行端口,写一个仿真器为您的设备你的测试可以谈。在这个层面上,它更复杂(但不是不可能的)自动化测试。

If you want to actually emulate a device, then this goes beyond "unit testing." At this level, it's possible to use com0com to add a pair of virtual serial ports and write an emulator for your device that your tests can talk to. At this level, it's much more complex (though not impossible) to automate the testing.

这篇关于如何创建使用起订量库的SerialPort模拟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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