1

I am using Matlab's unittest to test the handling of invalid parameters.

In the test I have a line

t.verifyError(@myObject.myMethod, 'MATLAB:nonStrucReference'); 

which works fine in Matlab R2014a, but fails in Matlab R2016a with the message

--------------------- Framework Diagnostic: --------------------- verifyError failed. --> The function threw the wrong exception. Actual Exception: 'MATLAB:structRefFromNonStruct' Expected Exception: 'MATLAB:nonStrucReference' 

I wonder if it would be possible to test whether one of the exceptions is thrown.

I know that it would be possible to write

t.verifyError(@myObject.myMethod, ?MException); 

but something more specific would be better.

0

1 Answer 1

2

You would likely want to write a custom verification method which accepts a cell array of exceptions as the input.

function verifyOneOfErrors(testcase, func, identifiers, varargin) % Ensure that a cell array was passed rather than a string if ischar(identifiers) identifiers = {identifiers}; end % If the function succeeds with no errors, then we want a failure threw_correct_error = false; try func() catch ME % Check if the identifier is in our list of approved identifiers threw_correct_error = ismember(ME.identifier, identifiers); end % Do the actual verification testcase.verifyTrue(threw_correct_error, varargin{:}) end 

Another alternative is to actually get the error message identifier dynamically within your testcase by explicitly causing the error, and retrieving the identifier.

% Get a version-specific identifier for this specific error try; a = []; a.field; catch ME; end; % Verify that your method throws this error t.verifyError(@myObject.myMethod, ME.identifier) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.