Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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
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
returnStoredValueWhenReturn stored property value
storeValueWhenStore property value
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 routine
applyFixtureUse fixture with TestCase
forInteractiveUseCreate TestCase for interactive use
getSharedTestFixturesProvide access to shared test fixtures
logRecord diagnostic information
onFailureDynamically add diagnostics for test failures
runRun TestCase test

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

expand 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));

Introduced in R2017a

Was this topic helpful?