assertCalled

Class: matlab.mock.TestCase
Package: matlab.mock

Assert that a method was called with certain input values

Description

example

assertCalled(testcase,behavior) asserts that a method was called with certain input values.

example

assertCalled(testcase,behavior,diagnostic) also displays the diagnostic information in diagnostic upon a failure.

Input Arguments

expand all

Instance of the test case, specified as a matlab.mock.TestCase object.

Behavior of the mock, specified as a matlab.mock.MethodCallBehavior instance. To create an instance of matlab.mock.MethodCallBehavior, call a method of the behavior object.

Example: withExactInputs(myMockBehavior.myMockedMethod)

Diagnostic information to display, specified as a string array, character array, function handle, or matlab.unittest.diagnostics.Diagnostic object. Diagnostic values can be nonscalar. For more information, see matlab.unittest.diagnostics.Diagnostic.

Example: "My diagnostic message."

Example: @() datetime('now')

Examples

expand all

Create a mock with an implicit interface for interactive use. The interface includes the foo and bar methods. Call foo by specifying an input.

testCase = matlab.mock.TestCase.forInteractiveUse;
[mock,behavior] = testCase.createMock('AddedMethods',["foo","bar"]);
mock.foo(123);

Assert that foo was called with input 123.

testCase.assertCalled(behavior.foo(123));
Assertion passed.

Create a mock with an implicit interface for interactive use. The interface includes the foo and bar methods. Call foo by specifying an input.

testCase = matlab.mock.TestCase.forInteractiveUse;
[mock,behavior] = testCase.createMock('AddedMethods',["foo","bar"]);
mock.foo(123);

Assert that foo was called with input 456. Display the diagnostic information upon failure.

testCase.assertCalled(behavior.foo(456), ...
    'Method foo should have been called with input 456.');
Assertion failed.
    ----------------
    Test Diagnostic:
    ----------------
    Method foo should have been called with input 456.
    ---------------------
    Framework Diagnostic:
    ---------------------
    assertCalled failed.
    --> Method 'foo' was not called with the specified signature.
    --> All observed method call(s) with any signature are:
            foo([1×1 matlab.mock.classes.Mock], 123)
    
    Specified method call:
    MethodCallBehavior
        [...] = foo(<Mock>, 456)
Assertion failed.

Assert that bar was called at least once with only the object as an input.

testCase.assertCalled(withExactInputs(behavior.bar), ...
    'Method bar should have been called.');
Assertion failed.
    ----------------
    Test Diagnostic:
    ----------------
    Method bar should have been called.
    ---------------------
    Framework Diagnostic:
    ---------------------
    assertCalled failed.
    --> Method 'bar' was never called.
    
    Specified method call:
    MethodCallBehavior
        [...] = bar(<Mock>)
Assertion failed.

Tips

Use assertion qualifications when the 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.

  • Use verification qualifications to 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.

  • Use assumption qualifications to 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.

  • Use fatal assertion qualifications to 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.

Alternatives

Using the assertCalled method is functionally equivalent to using the matlab.mock.constraints.WasCalled constraint with the assertThat method of the Assertable class. For example, the following code blocks are functionally equivalent.

% Using the assertCalled method
testCase.assertCalled(behavior.foo(123), ...
    'Method foo should have been called with input 123.')

% Using the WasCalled constraint with assertThat method
import matlab.mock.constraints.WasCalled;
testCase.assertThat(behavior.foo(123),WasCalled, ...
    'Method foo should have been called with input 123.');
However, there is more functionality when you use the WasCalled constraint. For instance, you can specify a method was called a certain number of times.

Introduced in R2017a