matlab.unittest.constraints.Throws 类
命名空间: matlab.unittest.constraints
超类: matlab.unittest.constraints.Constraint
测试函数是否抛出指定的错误
描述
matlab.unittest.constraints.Throws
类提供一个约束来测试函数句柄是否抛出指定的错误。
matlab.unittest.constraints.Throws
类是一个 handle
类。
创建对象
描述
c = matlab.unittest.constraints.Throws(
创建一个约束来测试函数句柄是否抛出 identifier
)identifier
指定的错误。如果实际值是在被测试框架调用时抛出指定错误的函数句柄,则满足约束。
c = matlab.unittest.constraints.Throws(
使用一个或多个名称-值参量指定选项。例如,identifier
,Name,Value
)c = matlab.unittest.constraints.Throws(identifier,"WhenNargoutIs",2)
创建一个约束来测试某函数句柄在用两个输出参量调用时是否会抛出指定的错误。
输入参量
错误标识符,指定为字符串标量、字符向量或 matlab.metadata.Class
实例。
如果 identifier
是 matlab.metadata.Class
实例,且如果抛出的错误指定类的实例或其子类之一,则满足约束。
此参量设置 ExpectedException
属性。
示例: "MATLAB:UndefinedFunction"
示例: ?MException
名称-值参数
以 Name1=Value1,...,NameN=ValueN
的形式指定可选参量对组,其中 Name
是参量名称,Value
是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。
示例: c = matlab.unittest.constraints.Throws(identifier,WhenNargoutIs=2)
在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name
引起来。
示例: c = matlab.unittest.constraints.Throws(identifier,"WhenNargoutIs",2)
调用函数句柄时约束请求的输出数目,指定为非负整数标量。
此参量设置 Nargout
属性。
数据类型: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
预期错误的必需原因,指定为字符串数组、字符向量元胞数组或由 matlab.metadata.Class
实例组成的数组。如果函数句柄抛出错误时缺失任何指定的原因,则不满足约束。
此参量设置 RequiredCauses
属性。
是否遵从一组必需的原因,指定为数值或逻辑值 0
(false
) 或 1
(true
)。如果值为 true
,且预期的错误包含 CausedBy
名称-值参量未指定的原因,则不满足约束。默认情况下,约束对其他原因不敏感。
此参量设置 RespectSet
属性。
属性
预期错误的标识符,以字符向量或 matlab.metadata.Class
实例形式返回。
此属性由 identifier
输入参量设置。
属性:
GetAccess | public |
SetAccess | immutable |
预期错误的必需原因,以字符向量元胞数组或由 matlab.metadata.Class
实例组成的数组形式返回。
此属性由 CausedBy
名称-值参量设置。
属性:
GetAccess | public |
SetAccess | private |
是否遵从一组必需的原因,以数据类型为 logical
的 0
或 1
形式返回。
此属性由 RespectingSet
名称-值参量设置。
属性:
GetAccess | public |
SetAccess | private |
示例
测试 actual 值是否为抛出指定错误的函数句柄。
首先,导入此示例中使用的类。
import matlab.unittest.TestCase import matlab.unittest.constraints.Throws
创建一个供交互测试的测试用例。
testCase = TestCase.forInteractiveUse;
验证 error
函数抛出具有预期标识符的错误。
testCase.verifyThat(@() error("SOME:error:id","Error!"), ... Throws("SOME:error:id"))
Verification passed.
通过将约束传递给 matlab.metadata.Class
实例而不是字符串,再次进行测试。测试通过。
testCase.verifyThat(@() error("SOME:error:id","Error!"), ... Throws(?MException))
Verification passed.
如果实际和预期的错误标识符不匹配,则验证测试失败。
testCase.verifyThat(@() error("SOME:error:id","Error!"), ... Throws("OTHER:error:id"))
Verification failed. --------------------- Framework Diagnostic: --------------------- Throws failed. --> The function threw the wrong exception. Actual Exception: 'SOME:error:id' Expected Exception: 'OTHER:error:id' --> Actual Error Report: Error using @()error("SOME:error:id","Error!") Error! Evaluated Function: function_handle with value: @()error("SOME:error:id","Error!")
测试 rand
函数。测试失败,因为 rand
没有抛出任何错误。
testCase.verifyThat(@rand,Throws(?MException))
Verification failed. --------------------- Framework Diagnostic: --------------------- Throws failed. --> The function did not throw any exception. Expected Exception: ?MException Evaluated Function: function_handle with value: @rand
disp
函数不返回任何输出。验证用输出参量调用 disp
会抛出指定的错误。
testCase.verifyThat(@() disp("Hello World!"), ... Throws("MATLAB:maxlhs","WhenNargoutIs",1))
Verification passed.
验证如果实际值不是函数句柄,则不满足 Throws
约束。
testCase.verifyThat(5,Throws("SOME:error:id"))
Verification failed. --------------------- Framework Diagnostic: --------------------- Throws failed. --> The value must be an instance of the expected type. Actual Class: double Expected Type: function_handle Actual Value: 5
使用 Throws
约束测试错误原因。
首先,导入此示例中使用的类。
import matlab.unittest.TestCase import matlab.unittest.constraints.Throws
创建一个供交互测试的测试用例。
testCase = TestCase.forInteractiveUse;
创建一个 MException
对象 me
,有两个错误原因 cause1
和 cause2
。
me = MException("TOP:id","Top-Level Error!"); cause1 = MException("CAUSE1:id1","First Cause of Error!"); cause2 = MException("CAUSE2:id2","Second Cause of Error!"); me = me.addCause(cause1); me = me.addCause(cause2);
验证当框架抛出 cause1
时它是 me
的原因。
testCase.verifyThat(@() me.throw,Throws("TOP:id","CausedBy","CAUSE1:id1"))
Verification passed.
通过将原因指定为 matlab.metadata.Class
实例,再次进行测试。测试通过。如果 me
没有原因,测试将失败。
testCase.verifyThat(@() me.throw,Throws("TOP:id","CausedBy",?MException))
Verification passed.
测试除了 cause1
之外,错误是否没有其他原因。测试失败,因为 cause2
也是预期错误的原因。
testCase.verifyThat(@() me.throw, ... Throws("TOP:id","CausedBy","CAUSE1:id1","RespectingSet",true))
Verification failed. --------------------- Framework Diagnostic: --------------------- Throws failed. --> The following causes were unexpectedly found in the exception tree: --> 'CAUSE2:id2' --> Actual Error Report: Error using @()me.throw Top Level Error! Caused by: First Cause of Error! Second Cause of Error! Actual Error Structure: ?MException 'TOP:id' --> ?MException 'CAUSE1:id1' --> ?MException 'CAUSE2:id2' Evaluated Function: function_handle with value: @()me.throw
验证如果指定的原因不属于错误,是否不满足约束。
testCase.verifyThat(@() error("TOP:id","Top-Level Error!"), ... Throws("TOP:id","CausedBy","CAUSE1:id1"))
Verification failed. --------------------- Framework Diagnostic: --------------------- Throws failed. --> The following causes were not found in the exception tree: --> 'CAUSE1:id1' --> Actual Error Report: Error using @()error("TOP:id","Top-Level Error!") Top-Level Error! Actual Error Structure: ?MException 'TOP:id' Evaluated Function: function_handle with value: @()error("TOP:id","Top-Level Error!")
版本历史记录
在 R2013a 中推出
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)