Main Content

assertCalled

类: matlab.mock.TestCase
命名空间: matlab.mock

断言使用特定输入值调用过方法

说明

示例

assertCalled(testcase,behavior) 断言使用特定输入值调用过某方法。

示例

assertCalled(testcase,behavior,diagnostic) 还将 diagnostic 中的诊断信息与鉴定相关联。根据测试运行器的配置,测试框架可能会在鉴定通过或失败时显示诊断信息。默认情况下,框架仅在鉴定失败时显示诊断信息。您可以通过自定义测试运行器来覆盖默认行为。例如,使用 DiagnosticsOutputPlugin 实例来显示失败和通过事件诊断信息。

输入参数

全部展开

测试用例的实例,指定为 matlab.mock.TestCase 对象。

mock 的行为,指定为 matlab.mock.MethodCallBehavior 实例。要创建 matlab.mock.MethodCallBehavior 实例,请调用行为对象的方法。

示例: withExactInputs(myMockBehavior.myMockedMethod)

要显示的诊断信息,指定为字符串数组、字符数组、函数句柄或 matlab.automation.diagnostics.Diagnostic 对象。诊断值可以是非标量。有关详细信息,请参阅 matlab.automation.diagnostics.Diagnostic

示例: "My diagnostic message."

示例: @() datetime('now')

示例

全部展开

创建一个具有隐式接口的 mock 以供交互使用。该接口包括 foobar 方法。通过指定输入调用 foo

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

断言使用输入 123 调用过 foo

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

创建一个具有隐式接口的 mock 以供交互使用。该接口包括 foobar 方法。通过指定输入调用 foo

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

断言使用输入 456 调用过 foo。在失败时显示诊断信息。

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.

断言至少只使用对象作为输入调用过一次 bar

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.

提示

当失败条件导致当前测试内容的剩余部分都失效,但不会阻止后续测试方法正确执行时,使用断言鉴定。断言点处的失败会将当前测试方法标记为失败且未完成。

  • 使用此确认在不引发异常的条件下生成和记录失败。由于确认不会引发异常,因此即使出现确认失败的情形,依然会完成所有的测试内容。通常,确认是单元测试的主要鉴定类型,因为这些确认一般不要求提前从测试中退出。使用其他鉴定类型来测试是否违反先决条件或测试安装是否正确。

  • 使用断言鉴定确保测试环境满足无论如何都不会导致测试失败的先决条件。假设失败会生成已过滤的测试,且测试框架会将测试设为 Incomplete

  • 使用致命断言鉴定在失败时中止测试会话。当失败涉及根本以致没必要继续测试时,这种鉴定会很有用。当脚手架拆解未能正确还原 MATLAB® 状态,适合中止测试并启动一个新会话时,这些鉴定也很有用。

备选方法

使用 assertCalled 方法在功能上等同于对 Assertable 类的 assertThat 方法使用 matlab.mock.constraints.WasCalled 约束。例如,以下代码块在功能上是等效的。

% 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.');
不过,在使用 WasCalled 约束时,会有更多功能。例如,您可以指定某方法被调用特定次数。

版本历史记录

在 R2017a 中推出