Main Content

matlab.mock.constraints.WasSet 类

命名空间: matlab.mock.constraints

用于确定属性设置交互情况的约束

描述

如果实际值不是 PropertyBehavior 实例,或者 PropertyBehavior 对应属性的设置次数不是指定的次数,则 WasSet 约束将发生验证失败。

构造

constraint = WasSet 创建约束以确定属性的 set 交互。如果属性值至少被设置过一次,则满足该约束。要验证属性未被设置过,可使用波浪号 (~) 运算符对 WasSet 约束求反。

constraint = WasSet(Name,Value) 提供具有额外选项的约束,这些选项由一个或多个 Name,Value 对组参数指定。例如,WasSet('ToValue',42) 构造一个约束,当属性值被设置为 42 时将满足该约束,WasSet('ToValue',42,'WithCount',3) 构造一个约束,当属性值正好 3 次被设置为 42 时将满足该约束。

输入参数

全部展开

名称-值参数

将可选的参数对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参数名称,Value 是对应的值。名称-值参数必须出现在其他参数之后,但参数对组的顺序无关紧要。

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

指定的属性值,指定为标量、向量、矩阵或多维数组。值可以是任何数据类型,并且与行为指定的属性相关。

示例: 'Joe'

示例: [1 2 3;4 5 6]

属性的设置次数,指定为整数。

如果您通过此语法对 WasSet 求反,则当属性值的设置次数不等于 n 时,约束将通过。例如,如果属性被设置四次,~WasSet('WithCount',3) 将通过,~WasSet('WithCount',4) 将失败。

示例: 5

属性

全部展开

属性值,指定为标量、向量、矩阵或多维数组。值可以是任何数据类型,并且与行为指定的属性相关。

属性设置计数,以整数形式返回。一旦约束构造完毕,此属性即变成只读属性。您可以在构造约束的过程中指定它。

复制语义

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

示例

全部折叠

为 person 类创建一个 mock。

testCase = matlab.mock.TestCase.forInteractiveUse;
[fakePerson,behavior] = testCase.createMock('AddedProperties',["Name" "Age"]);

使用 mock。

fakePerson.Name = 'David';

构造通过用例。

import matlab.mock.constraints.WasSet
testCase.verifyThat(behavior.Name,WasSet)
Interactive verification passed.
testCase.verifyThat(behavior.Age,~WasSet)
Interactive verification passed.
testCase.verifyThat(behavior.Name,WasSet('ToValue','David'))
Interactive verification passed.
testCase.verifyThat(behavior.Name,WasSet('WithCount',1))
Interactive verification passed.

构造失败用例。

testCase.verifyThat(behavior.Name,~WasSet)
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
Negated WasSet failed.
--> Property 'Name' was unexpectedly set to the specified value 1 time(s).
--> Observed property set(s) to any value:
        <Mock>.Name = 'David'

Specified property set:
    PropertySetBehavior
        <Mock>.Name = <IsAnything constraint>
testCase.verifyThat(behavior.Age,WasSet)
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
WasSet failed.
--> Property 'Age' was never set.

Specified property set:
    PropertySetBehavior
        <Mock>.Age = <IsAnything constraint>
testCase.verifyThat(behavior.Name,WasSet('ToValue','Andy'))
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
WasSet failed.
--> Property 'Name' was not set to the specified value.
--> Observed property set(s) to any value:
        <Mock>.Name = 'David'

Specified property set:
    PropertySetBehavior
        <Mock>.Name = 'Andy'
testCase.verifyThat(behavior.Name,WasSet('WithCount',5))
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
WasSet failed.
--> Property 'Name' was not set to the specified value the expected number of times.
    
    Actual property set count:
             1
    Expected property set count:
             5
--> Observed property set(s) to any value:
        <Mock>.Name = 'David'

Specified property set:
    PropertySetBehavior
        <Mock>.Name = <IsAnything constraint>

版本历史记录

在 R2017a 中推出