Main Content

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

matlab.unittest.constraints.IsOfClass 类

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

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

描述

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

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

创建对象

描述

示例

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

属性

全部展开

预期的类,以字符向量形式返回。在创建约束的过程中,将此属性的值指定为字符串标量、字符向量或 meta.class 实例。

属性:

GetAccess
public
SetAccess
private

示例

全部折叠

使用 IsOfClass 约束测试数值。

首先,导入此示例中使用的类。

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsOfClass

创建一个供交互测试的测试用例。

testCase = TestCase.forInteractiveUse;

验证数值 5 的类是 double

testCase.verifyThat(5,IsOfClass("double"))
Verification passed.

使用 meta.class 实例而不是字符串重复该测试。

testCase.verifyThat(5,IsOfClass(?double))
Verification passed.

验证零不是逻辑值。

testCase.verifyThat(0,~IsOfClass("logical"))
Verification passed.

使用 IsOfClass 约束测试函数句柄。

首先,导入此示例中使用的类。

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsOfClass

创建一个供交互测试的测试用例。

testCase = TestCase.forInteractiveUse;

验证 @sin 是否为函数句柄。

testCase.verifyThat(@sin,IsOfClass(?function_handle))
Verification passed.

使用函数名称 "sin" 重复该测试。测试失败。

testCase.verifyThat("sin",IsOfClass(?function_handle))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsOfClass failed.
    --> The value's class is incorrect.
        
        Actual Class:
            string
        Expected Class:
            function_handle
    
    Actual Value:
        "sin"

使用 IsOfClass 约束测试派生类的实例。

在当前文件夹下的文件中,创建 ExampleHandle 句柄类。

classdef ExampleHandle < handle
    properties
        Number = 1;
    end
end

导入此示例中使用的类。

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsOfClass

创建一个供交互测试的测试用例。

testCase = TestCase.forInteractiveUse;

使用 IsOfClass 约束测试 ExampleHandle 对象。测试通过。

actual = ExampleHandle;
testCase.verifyThat(actual,IsOfClass(?ExampleHandle))
Verification passed.

使用 handle 类重复该测试。尽管 actual 派生自 handle 类,测试仍失败,因为 handle 不是 actual 的完全匹配类。

testCase.verifyThat(actual,IsOfClass(?handle))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsOfClass failed.
    --> The value's class is incorrect.
        
        Actual Class:
            ExampleHandle
        Expected Class:
            handle
    
    Actual Value:
      ExampleHandle with properties:
    
        Number: 1

使用 IsOfClass 约束测试函数的输出。

在当前文件夹的一个文件中创建 add5 函数。该函数接受数值输入,并将其增加 5。

function y = add5(x)
% add5 - Increment input by 5
if ~isa(x,"numeric")
    error("add5:InputMustBeNumeric","Input must be numeric.")
end
y = x + 5;
end

导入此示例中使用的类。

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsOfClass

创建一个供交互测试的测试用例。

testCase = TestCase.forInteractiveUse;

使用有效输入调用函数。然后,测试返回值的类是否为 double。测试通过。

actual = add5(1);
testCase.verifyThat(actual,IsOfClass(?double))
Verification passed.

版本历史记录

在 R2013a 中推出