A mock object library for MATLAB.
I initially wrote MockObject to help me unit test code that connected to hardware devices using COM objects. By swapping in a mock for the actual COM object, I could could simulate any device behavior I wanted.
I've since used it to mock HTTP sessions as well, and it's probably useful wherever you want to mock out Something Big.
The MockObject class has two main features:
- It maintains a lookup table of function return values: for any function, for a specific function, or for a specific function with specific arguments
- It maintains a list of what methods were called, along with the arguments used.
To add MockObject to your MATLAB path, run onLoad
. To remove it from your path, run onUnload
.
- Create a subclass of
MockObject
.(Let's call itMockFoo
.) Add functions to this subclass that match the signatures of the functions in the class you're mocking(RealFoo
). Each function inMockFoo
will make a call toaddToCallStack
to record that it's been called, and a call togetReturnValue
to find out what it should return. - At runtime, create an instance of
MockFoo
. CalladdReturnValueEntry
to add values to the return value lookup table. - Use
MockFoo
in place ofRealFoo
. - Inspect the
MockFoo.CallStack
property to see what methods were called.
See the example subdirectory for an example of how this would work.
- Supported on Matlab R2012a and newer. (May work on older versions but no guarantees.)
- matlab-xunit-4.0.0 if you want to run the unit tests.
The code for this project lives at GitHub.
To file a bug report or feature request, create an issue there.
Licensed under the BSD license. See the LICENSE file in this directory.
Last Updated:
Paul Sexton
2014-10-02