matlab.unittest.constraints.IsOfClass 类
命名空间: matlab.unittest.constraints
超类: matlab.unittest.constraints.BooleanConstraint
测试值的类是否为指定的类
描述
matlab.unittest.constraints.IsOfClass
类提供一个约束来测试值的类是否为指定的类。
IsOfClass
约束测试是否为完全匹配类。要测试是否涵盖在类层次结构中,请使用 IsInstanceOf
约束。
创建对象
属性
Class
— 预期的类
字符向量
预期的类,以字符向量形式返回。在创建约束的过程中,将此属性的值指定为字符串标量、字符向量或 matlab.metadata.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.
使用 matlab.metadata.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 中推出
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)