matlab.unittest.constraints.IsInstanceOf 类
命名空间: matlab.unittest.constraints
超类: matlab.unittest.constraints.BooleanConstraint
测试值是否为指定类的实例
描述
matlab.unittest.constraints.IsInstanceOf
类提供一个约束来测试值是否为指定类的实例。
IsInstanceOf
约束测试值是否涵盖在类层次结构中。要测试是否完全匹配类,请使用 IsOfClass
约束。
创建对象
属性
Class
— 预期的类
字符向量
预期的类,以字符向量形式返回。在创建约束的过程中,将此属性的值指定为字符串标量、字符向量或 matlab.metadata.Class
实例。
属性:
GetAccess | public |
SetAccess | private |
示例
测试数值
使用 IsInstanceOf
约束测试数值。
首先,导入此示例中使用的类。
import matlab.unittest.TestCase import matlab.unittest.constraints.IsInstanceOf
创建一个供交互测试的测试用例。
testCase = TestCase.forInteractiveUse;
验证值 1
是类 double
的实例。
testCase.verifyThat(1,IsInstanceOf("double"))
Verification passed.
使用 matlab.metadata.Class
实例而不是字符串重复该测试。
testCase.verifyThat(1,IsInstanceOf(?double))
Verification passed.
验证数值是 logical
类的实例。
testCase.verifyThat(1,~IsInstanceOf("logical"))
Verification passed.
测试函数句柄
使用 IsInstanceOf
约束测试函数句柄。
首先,导入此示例中使用的类。
import matlab.unittest.TestCase import matlab.unittest.constraints.IsInstanceOf
创建一个供交互测试的测试用例。
testCase = TestCase.forInteractiveUse;
验证 @sin
是否为函数句柄。
testCase.verifyThat(@sin,IsInstanceOf(?function_handle))
Verification passed.
使用函数名称 "sin"
重复该测试。测试失败。
testCase.verifyThat("sin",IsInstanceOf(?function_handle))
Verification failed. --------------------- Framework Diagnostic: --------------------- IsInstanceOf failed. --> The value must be an instance of the expected type. Actual Class: string Expected Type: function_handle Actual Value: "sin"
测试派生类的实例
使用 IsInstanceOf
约束测试派生类的实例。
在当前文件夹下的文件中,创建 ExampleHandle
句柄类。
classdef ExampleHandle < handle properties Number = 1; end end
导入此示例中使用的类。
import matlab.unittest.TestCase import matlab.unittest.constraints.IsInstanceOf
创建一个供交互测试的测试用例。
testCase = TestCase.forInteractiveUse;
使用 IsInstanceOf
约束测试 ExampleHandle
类的实例。测试通过。
actual = ExampleHandle; testCase.verifyThat(actual,IsInstanceOf(?ExampleHandle))
Verification passed.
测试 actual
是否也是 handle 类的实例。测试通过,因为 ExampleHandle
是从 handle 类派生的。
testCase.verifyThat(actual,IsInstanceOf(?handle))
Verification passed.
版本历史记录
在 R2013a 中推出
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)