Main Content

matlab.mock.TestCase class

Package: matlab.mock
Superclasses: matlab.unittest.TestCase

TestCase to write tests with mocking framework

Description

Use the matlab.mock.TestCase class to write tests that use the mocking framework. The matlab.mock.TestCase derives from the matlab.unittest.TestCase class.

Construction

The testing framework constructs the matlab.mock.TestCase instances.

Methods

assertAccessedAssert that a property was accessed
assertCalledAssert that a method was called with certain input values
assertNotAccessedAssert that a property was not accessed
assertNotCalledAssert that a method was not called with certain input values
assertNotSetAssert that a property was not set
assertSetAssert that a property was set
assignOutputsWhenDefine return values for method call or property access
assumeAccessedAssume that a property was accessed
assumeCalledAssume that a method was called with certain input values
assumeNotAccessedAssume that a property was not accessed
assumeNotCalledAssume that a method was not called with certain input values
assumeNotSetAssume that a property was not set
assumeSetAssume that a property was set
clearMockHistoryClear history of mock object interactions
createMockCreate mock object
fatalAssertAccessedFatally assert that a property was accessed
fatalAssertCalledFatally assert that a method was called with certain input values
fatalAssertNotAccessedFatally assert that a property was not accessed
fatalAssertNotCalledFatally assert that a method was not called with certain input values
fatalAssertNotSetFatally assert that a property was not set
fatalAssertSetFatally assert that a property was set
forInteractiveUseCreate TestCase for interactive use of mock objects
getMockHistoryReturn history of mock interactions from TestCase instance
returnStoredValueWhenReturn stored value when property is accessed
storeValueWhenStore value when property is set
throwExceptionWhenThrow exception for method call or property interaction
verifyAccessedVerify that a property was accessed
verifyCalledVerify that a method was called with certain input values
verifyNotAccessedVerify that a property was not accessed
verifyNotCalledVerify that a method was not called with certain input values
verifyNotSetVerify that a property was not set
verifySetVerify that a property was set

Inherited Methods

addTeardownDynamically add teardown code to test case
applyFixture Use fixture with test case
createTemporaryFolder Create temporary folder
forInteractiveUseCreate test case for interactive use
getSharedTestFixtures Provide access to shared test fixtures
logRecord diagnostic information during test execution
onFailureDynamically add diagnostics for test failures
runRun tests corresponding to test case

Also, the TestCase class inherits methods from these classes:

matlab.unittest.qualifications.AssertableQualification to validate preconditions of a test
matlab.unittest.qualifications.AssumableQualification to filter test content
matlab.unittest.qualifications.FatalAssertableQualification to abort test execution
matlab.unittest.qualifications.VerifiableQualification to produce soft-failure conditions

Copy Semantics

Handle. To learn how handle classes affect copy operations, see Copying Objects.

Examples

collapse all

Write a test using a mock.

import matlab.unittest.constraints.IsLessThan;
testCase = matlab.mock.TestCase.forInteractiveUse;

% Create a mock for a bank account class
[mock, behavior] = testCase.createMock('AddedMethods',["deposit" "isOpen"]);

% Set up behavior
testCase.throwExceptionWhen(behavior.deposit(IsLessThan(0)), ...
    MException('Account:deposit:Negative', ...
    'Deposit amount must be positive.'));

% Use mock object
mock.deposit(100);
testCase.verifyError(@() mock.deposit(-10), 'Account:deposit:Negative');

% Passing verifications
testCase.verifyCalled(behavior.deposit(100),...
    'A $100 deposit should have been made.');
testCase.assertNotCalled(behavior.deposit(0));
testCase.assertCalled(behavior.deposit(IsLessThan(0)));

% Failing assertion
testCase.assertCalled(withExactInputs(behavior.isOpen));

Version History

Introduced in R2017a