Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use mock objects to mock a serial port?

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.

What I would like to know is:

  • 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.
like image 699
DPD Avatar asked Apr 20 '11 09:04

DPD


1 Answers

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?

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

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

  • Hardware driver layer
  • Control layer
  • API layer for outside modules

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

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

Is Rhino Mocks suitable for mocking a C++/CLI application ?

Not sure about this

Any other mocking frameworks (prefereably freeware but open to paid ones) that in your experience is good for this task?

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.

Are these comepatible with MS Test?

Sorry don't know this one either

like image 56
ajdiperna Avatar answered Sep 24 '22 14:09

ajdiperna