Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

matlab.unittest.constraints.IsInstanceOf 类

命名空间: matlab.unittest.constraints
超类: matlab.unittest.constraints.BooleanConstraint

测试值是否为指定类的实例

描述

matlab.unittest.constraints.IsInstanceOf 类提供一个约束来测试值是否为指定类的实例。

IsInstanceOf 约束测试值是否涵盖在类层次结构中。要测试是否完全匹配类,请使用 IsOfClass 约束。

创建对象

描述

示例

c = matlab.unittest.constraints.IsInstanceOf(class) 创建一个约束来测试某个值是否为 class 的实例并设置 Class 属性。如果该值是 class 的实例或派生自 class,则满足该约束。

属性

全部展开

预期的类,以字符向量形式返回。在创建约束的过程中,将此属性的值指定为字符串标量、字符向量或 meta.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.

使用 meta.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 中推出