Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a SerialPort mock using the Moq library?

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?

like image 973
Padu Merloti Avatar asked May 27 '10 16:05

Padu Merloti


People also ask

What can be mocked with MOQ?

Moq can be used to mock both classes and interfaces.

What is MOQ mocking framework?

Moq is a mocking framework built to facilitate the testing of components with dependencies. As shown earlier, dealing with dependencies could be cumbersome because it requires the creation of test doubles like fakes. Moq makes the creation of fakes redundant by using dynamically generated types.

What is mock used for C#?

Mocking is a process that allows you to create a mock object that can be used to simulate the behavior of a real object. You can use the mock object to verify that the real object was called with the expected parameters, and to verify that the real object was not called with unexpected parameters.

What is mock test in .NET core?

A mock version of something is an object that can act like the real thing but can be controlled in test code. Moq (pronounced “mok u” or “mock”) is a library available on NuGet that allows mock objects to be created in test code and it also supports . NET Core.


1 Answers

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

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.

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.

like image 51
Stephen Cleary Avatar answered Sep 24 '22 11:09

Stephen Cleary