Remember, you must pass a matlab.unittest.TestCase Class object into assertError as an input argument. You also need to call it with a function handle that assertError can call. tc = matlab.unittest.TestCase.forInteractiveUse;
If you don't use an error ID in your error call, tell assertError that as long as some error gets thrown the test should pass.
assertError(tc, @myfunctionThatThrowsAnErrorWithoutErrorID, ?MException)
If you do use an error ID, you can specify that error ID to check for that specific error.
assertError(tc, @myfunctionThatThrowsAnErrorWithErrorID, 'MATLABAnswers:specificErrorID')
If a different error gets thrown, this test case fails. I'm using verifyError instead of assertError from this point on so that MATLAB doesn't stop executing the code, and you can see the next three test cases fail. But assertError would behave the same (except throwing a hard error instead of simply recording a test failure and moving on.)
verifyError(tc, @myfunctionThatThrowsAnErrorWithErrorID, 'MATLABAnswers:incorrectErrorID')
Verification failed.
---------------------
Framework Diagnostic:
---------------------
verifyError failed.
--> The function threw the wrong exception.
Actual Exception:
'MATLABAnswers:specificErrorID'
Expected Exception:
'MATLABAnswers:incorrectErrorID'
--> Actual Error Report:
Error using LiveEditorEvaluationHelperEeditorId>myfunctionThatThrowsAnErrorWithErrorID (line 16)
This is my error
Evaluated Function:
function_handle with value:
@myfunctionThatThrowsAnErrorWithErrorID
------------------
Stack Information:
------------------
In /tmp/Editor_hgeli/LiveEditorEvaluationHelperEeditorId.m (LiveEditorEvaluationHelperEeditorId) at 4
In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalMatlab.p (fevalMatlab) at 0
In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalJSON.p (fevalJSON) at 0
If no error gets thrown, the test case also fails regardless of whether you're checking for any error or a specific error.
verifyError(tc, @myfunctionThatDoesNotError, ?MException)
Verification failed.
---------------------
Framework Diagnostic:
---------------------
verifyError failed.
--> The function did not throw any exception.
Expected Exception:
?MException
Evaluated Function:
function_handle with value:
@myfunctionThatDoesNotError
------------------
Stack Information:
------------------
In /tmp/Editor_hgeli/LiveEditorEvaluationHelperEeditorId.m (LiveEditorEvaluationHelperEeditorId) at 5
In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalMatlab.p (fevalMatlab) at 0
In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalJSON.p (fevalJSON) at 0
verifyError(tc, @myfunctionThatDoesNotError, 'MATLABAnswers:specificErrorID')
Verification failed.
---------------------
Framework Diagnostic:
---------------------
verifyError failed.
--> The function did not throw any exception.
Expected Exception:
'MATLABAnswers:specificErrorID'
Evaluated Function:
function_handle with value:
@myfunctionThatDoesNotError
------------------
Stack Information:
------------------
In /tmp/Editor_hgeli/LiveEditorEvaluationHelperEeditorId.m (LiveEditorEvaluationHelperEeditorId) at 6
In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalMatlab.p (fevalMatlab) at 0
In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalJSON.p (fevalJSON) at 0
If you call the function inside assertError, MATLAB would run that function to try to use its output as the function to check for an error. Yes, you can have a function handle that returns a function handle when evaluated. In this example, when f is run it returns a function handle that will create two vectors and try to add them together.
f = @(x) @() ones(1, 5) + ones(1, x);
verifyError(tc, f(2), 'MATLAB:sizeDimensionsMustMatch')
verifyError(tc, f(10), 'MATLAB:sizeDimensionsMustMatch')
verifyError(tc, f(5), 'MATLAB:sizeDimensionsMustMatch')
Verification failed.
---------------------
Framework Diagnostic:
---------------------
verifyError failed.
--> The function did not throw any exception.
Expected Exception:
'MATLAB:sizeDimensionsMustMatch'
Evaluated Function:
function_handle with value:
@()ones(1,5)+ones(1,x)
------------------
Stack Information:
------------------
In /tmp/Editor_hgeli/LiveEditorEvaluationHelperEeditorId.m (LiveEditorEvaluationHelperEeditorId) at 10
In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalMatlab.p (fevalMatlab) at 0
In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalJSON.p (fevalJSON) at 0
function myfunctionThatThrowsAnErrorWithoutErrorID()
error('This is my error')
function myfunctionThatThrowsAnErrorWithErrorID()
error('MATLABAnswers:specificErrorID', 'This is my error')
function y = myfunctionThatDoesNotError()