setToValue
类: matlab.mock.PropertyBehavior
包: matlab.mock
构造对象,以定义当模拟属性设置为特定值时的行为
语法
setBehavior = setToValue(behavior,value)
说明
setBehavior = setToValue(
构造 behavior
,value
)PropertySetBehavior
对象,以定义当模拟属性设置为特定值时的行为。通常情况下,当您定义 mock 行为时,可使用 setToValue
方法来隐式构造 PropertySetBehavior
。
输入参数
behavior
— mock 的行为
matlab.mock.PropertyBehavior
实例
mock 的行为,指定为 matlab.mock.PropertyBehavior
实例。要创建 matlab.mock.PropertyBehavior
实例,请访问行为对象的属性。
示例: myMockBehavior.MyProperty
value
— 属性值
标量 | 向量 | 矩阵 | 多维数组 | 约束 | 行为对象
mock 属性必须设置为的属性值,指定为标量、向量、矩阵、多维数组、约束或行为对象。值可以是任何数据类型,并且与行为指定的属性相关。
示例: "hello"
示例: 42
示例: [1 2 3]
示例: matlab.unittest.constraints.IsLessThan(10)
示例
为特定属性值定义行为
使用 Name
属性为 person 类创建一个 mock。
testCase = matlab.mock.TestCase.forInteractiveUse; [mock,behavior] = testCase.createMock('AddedProperties',"Name");
创建一个 PropertySetBehavior
对象并设置其行为。当您将 Name
属性的值设置为 "David"
时,mock 将引发异常。
setBehavior = setToValue(behavior.Name,"David");
testCase.throwExceptionWhen(setBehavior)
您也可以使用行为定义隐式创建 PropertySetBehavior
对象。
testCase.throwExceptionWhen(behavior.Name.setToValue("David"))
设置 Name
属性的值。
mock.Name = "Andy"; mock.Name = "David";
Error using matlab.mock.internal.MockContext/createMockObject/mockPropertySetCallback (line 420) The following property set was specified to throw an exception: <Mock>.Name = "David"
版本历史记录
在 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)