Main Content

matlab.mock.TestCase class

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

TestCaseto write tests with mocking framework

Description

Use thematlab.mock.TestCaseclass to write tests that use the mocking framework. Thematlab.mock.TestCase来源于matlab.unittest.TestCaseclass.

Construction

The testing framework constructs thematlab.mock.TestCaseinstances.

Methods

assertAccessed Assert that a property was accessed
assertCalled Assert that a method was called with certain input values
assertNotAccessed Assert that a property was not accessed
assertNotCalled Assert that a method was not called with certain input values
assertNotSet Assert that a property was not set
assertSet Assert that a property was set
assignOutputsWhen Define return values for method call or property access
assumeAccessed Assume that a property was accessed
assumeCalled Assume that a method was called with certain input values
assumeNotAccessed Assume that a property was not accessed
assumeNotCalled Assume that a method was not called with certain input values
assumeNotSet Assume that a property was not set
assumeSet 假设一个属性组
clearMockHistory Clear history of mock object interactions
createMock Create mock object
fatalAssertAccessed Fatally assert that a property was accessed
fatalAssertCalled Fatally assert that a method was called with certain input values
fatalAssertNotAccessed Fatally assert that a property was not accessed
fatalAssertNotCalled Fatally assert that a method was not called with certain input values
fatalAssertNotSet Fatally assert that a property was not set
fatalAssertSet Fatally assert that a property was set
forInteractiveUse CreateTestCasefor interactive use of mock objects
getMockHistory Return history of mock interactions fromTestCaseinstance
returnStoredValueWhen Return stored value when property is accessed
storeValueWhen Store value when property is set
throwExceptionWhen Throw exception for method call or property interaction
verifyAccessed Verify that a property was accessed
verifyCalled Verify that a method was called with certain input values
verifyNotAccessed Verify that a property was not accessed
verifyNotCalled Verify that a method was not called with certain input values
verifyNotSet Verify that a property was not set
verifySet Verify that a property was set

Inherited Methods

addTeardown Dynamically add teardown routine toTestCaseinstance
applyFixture Use fixture withTestCase
forInteractiveUse Create TestCase for interactive use
getSharedTestFixtures Provide access to shared test fixtures
log Record diagnostic information during test execution
onFailure Dynamically add diagnostics for test failures
run RunTestCasetest

Also, theTestCaseclass inherits methods from these classes:

Copy Semantics

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

Examples

collapse all

Write a test using a mock.

importmatlab.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 behaviortestCase.throwExceptionWhen(behavior.deposit(IsLessThan(0)),...MException('Account:deposit:Negative',...'Deposit amount must be positive.'));% Use mock objectmock.deposit(100); testCase.verifyError(@() mock.deposit(-10),'Account:deposit:Negative');% Passing verificationstestCase.verifyCalled(behavior.deposit(100),...'A $100 deposit should have been made.'); testCase.assertNotCalled(behavior.deposit(0)); testCase.assertCalled(behavior.deposit(IsLessThan(0)));% Failing assertiontestCase.assertCalled(withExactInputs(behavior.isOpen));
Introduced in R2017a