MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Qualify Mock Object Interaction


When you create a mock, you create an associated behavior object that controls mock
behavior. Use this object to access intercepted messages sent from the component under
test to the mock object (a process known as spying). For more information on creating a
mock, see “Create Mock Object” on page 33-200.

In the mocking framework, qualifications are functions used to test interactions with the
object. There are four types of qualifications:


  • Verifications — Produce and record failures without throwing an exception. Since
    verifications do not throw exceptions, all test content runs to completion even when
    verification failures occur. Typically verifications are the primary qualifications for a
    unit test since they typically do not require an early exit from the test. Use other
    qualification types to test for violation of preconditions or incorrect test setup.

  • Assumptions — Ensure that the test environment meets preconditions that otherwise
    do not result in a test failure. Assumption failures result in filtered tests, and the
    testing framework marks the tests as Incomplete.

  • Assertions — Ensure that a failure condition invalidates the remainder of the current
    test content, but does not prevent proper execution of subsequent test methods. A
    failure at the assertion point marks the current test method as failed and incomplete.

  • Fatal Assertions — Abort the test session upon failure. These qualifications are useful
    when the failure mode is so fundamental that there is no point in continuing testing.
    These qualifications are also useful when fixture teardown does not restore the
    MATLAB state correctly and it is preferable to abort testing and start a fresh session.


The mock object is an implementation of the abstract methods and properties of the
interface specified by a superclass. You can also construct a mock without a superclass, in
which case the mock has an implicit interface. Create a mock with an implicit interface
for a dice class. The interface includes Color and NumSides properties and a roll
method that accepts a number of dice and returns a value. While the interface is not
currently implemented, you can create a mock with it.

testCase = matlab.mock.TestCase.forInteractiveUse;
[mock,behaviorObj] = testCase.createMock('AddedProperties', ...
{'NumSides','Color'},'AddedMethods',{'roll'});

33 Unit Testing

Free download pdf