Main Content

matlab.mock.constraints.WasCalled 类

命名空间: matlab.mock.constraints

用于确定方法调用的约束

描述

如果实际值不是 MethodCallBehavior 实例,或者 MethodCallBehavior 对应方法的调用次数不是指定的次数,则 WasCalled 约束将发生验证失败。

构造

constraint = WasCalled 提供了一个约束,用以确定方法的调用情况。如果方法至少被调用过一次,则满足该约束。要验证方法未被调用过,可使用波浪号 (~) 运算符对 WasCalled 约束求反。

constraint = WasCalled('WithCount',n) 提供了一个约束,当方法的调用次数恰好为 n 次时,将满足该约束。

如果您通过此语法对 WasCalled 求反,则当方法的调用次数不等于 n 时,约束将通过。例如,如果方法被调用四次,~WasCalled('WithCount',3) 将通过,~WasCalled('WithCount',4) 将失败。

输入参数

全部展开

方法调用的次数,指定为整数。

属性

全部展开

方法调用计数,以整数形式返回。此属性是只读的。您可以在构造约束的过程中指定它。

复制语义

值。要了解值类如何影响复制操作,请参阅复制对象

示例

全部折叠

为 bank account 类创建一个 mock。

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

使用 mock 帐户。

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

构造通过用例。

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.

构造失败用例。

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

版本历史记录

在 R2017a 中推出