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)
将失败。
输入参量
n
— 方法调用的次数
整数
方法调用的次数,指定为整数。
属性
Count
— 方法调用计数
整数
方法调用计数,以整数形式返回。此属性是只读的。您可以在构造约束的过程中指定它。
复制语义
值。要了解值类如何影响复制操作,请参阅复制对象。
示例
限定 mock 方法的调用情况
为 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 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)