testCase.assignOutputsWhen(withAnyInputs(behaviorObj.findUser),"Unknown")
n = mock.findUser(1)
n =
"Unknown"
Specify that when the input value is 1701, the mock method returns "Jim". This behavior
supersedes the return of "Unknown" for the input value of 1701 only because it was
defined after that specification.
testCase.assignOutputsWhen(behaviorObj.findUser(1701),"Jim")
n = mock.findUser(1701)
n =
"Jim"
Specify that when the findUser method is called with only the object as input, the mock
method returns "Unspecified ID". The withExactInputs method of the
MethodCallBehavior object specifies that the behavior applies to a method call with
the object as the only input value.
testCase.assignOutputsWhen(withExactInputs(behaviorObj.findUser), ...
"Unspecified ID")
n = mock.findUser % equivalent to n = findUser(mock)
n =
"Unspecified ID"
You can use classes in the matlab.unittest.constraints package to help define
behavior. Specify that findUser throws an exception when it is called with an ID greater
than 5000.
import matlab.unittest.constraints.IsGreaterThan
testCase.throwExceptionWhen(behaviorObj.findUser(IsGreaterThan(5000)));
n = mock.findUser(5001)
Error using
matlab.mock.internal.MockContext/createMockObject/mockMethodCallback (line 323)
The following method call was specified to throw an exception:
findUser([1×1 matlab.mock.classes.Mock], 5001)
33 Unit Testing