Intelligent serial port mocks with Moq

Posted by Padu Merloti on Stack Overflow See other posts from Stack Overflow or by Padu Merloti
Published on 2010-05-27T16:46:47Z Indexed on 2010/05/27 20:01 UTC
Read the original article Hit count: 319

Filed under:
|
|
|
|

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.

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.

A simple example:

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.

Too much to ask?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET