matlab.mock.constraints.WasCalled class

Package: matlab.mock.constraints

Constraint determining method call

Description

The WasCalled constraint produces a qualification failure if an actual value is not a MethodCallBehavior instance, or if the method that corresponds to the MethodCallBehavior was not called the specified number of times.

Construction

constraint = WasCalled provides a constraint that determines a method call. If a method was called at least once, the constraint is satisfied. To qualify that a method was not called, negate the WasCalled constraint with the tilde (~) operator.

constraint = WasCalled('WithCount',n) provides a constraint that is satisfied when a method is called exactly n times.

If you negate WasCalled with this syntax, if the method was not called exactly n times, the constraint passes. For example, if a method was called four times, ~WasCalled('WithCount',3) passes and ~WasCalled('WithCount',4) fails.

Input Arguments

expand all

Number of method calls, specified as an integer.

Properties

expand all

Method call count, returned as an integer. This property is read-only. You can specify it during constraint construction.

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects.

Examples

collapse all

Create a mock for a bank account class.

testCase = matlab.mock.TestCase.forInteractiveUse;
[fakeAccount,behavior] = testCase.createMock('AddedMethods',"deposit");

Use the mock account.

fakeAccount.deposit(10);
fakeAccount.deposit(20);
fakeAccount.deposit(10);

Construct passing cases.

import matlab.mock.constraints.WasCalled
testCase.verifyThat(behavior.deposit(10),WasCalled)
Interactive verification passed.
testCase.verifyThat(behavior.deposit(10),WasCalled('WithCount',2))
Interactive verification passed.
import matlab.unittest.constraints.IsGreaterThan
testCase.verifyThat(behavior.deposit(IsGreaterThan(100)),~WasCalled)
Interactive verification passed.

Construct failing cases.

testCase.verifyThat(behavior.deposit(100),WasCalled);
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
WasCalled failed.
--> Method 'deposit' was not called with the specified signature.
--> Observed method call(s) with any signature:
        deposit([1×1 matlab.mock.classes.Mock], 10)
        deposit([1×1 matlab.mock.classes.Mock], 20)
        deposit([1×1 matlab.mock.classes.Mock], 10)

Specified method call:
    MethodCallBehavior
        [...] = deposit(<Mock>, 100)
testCase.verifyThat(behavior.deposit(20),WasCalled('WithCount',2))
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
WasCalled failed.
--> Method 'deposit' was not called the expected number of times with the specified signature.
    
    Actual method call count:
             1
    Expected method call count:
             2
--> Observed method call(s) with any signature:
        deposit([1×1 matlab.mock.classes.Mock], 10)
        deposit([1×1 matlab.mock.classes.Mock], 20)
        deposit([1×1 matlab.mock.classes.Mock], 10)

Specified method call:
    MethodCallBehavior
        [...] = deposit(<Mock, 20)
testCase.verifyThat(behavior.deposit(IsGreaterThan(50)),WasCalled)
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
WasCalled failed.
--> Method 'deposit' was not called with the specified signature.
--> Observed method call(s) with any signature:
        deposit([1×1 matlab.mock.classes.Mock], 10)
        deposit([1×1 matlab.mock.classes.Mock], 20)
        deposit([1×1 matlab.mock.classes.Mock], 10)

Specified method call:
    MethodCallBehavior
        [...] = deposit(<Mock>, <IsGreaterThan constraint>)

Introduced in R2017a