How to mock a serial port
This question came up on the Rhino Mocks list recently, and it interested me as the application we’re working on has to talk to serial ports too.
Two common themes emerged:
- Write a “thin” interface for serial port operations and create a thin wrapper class that implements this interface and calls SerialPort directly. You can then mock out the interface easily.
- To test the wrapper, you can make use of the com0com null-modem emulator.
I suspect there would still be a bit of effort to get the com0com emulator working properly, but it sounds promising.
Categories: .NET, Unit Testing