Thanks to the suport team, I was able to solve the problem.
The createMock object must be created as handle-class object by using ?handle as second input argument. The get value definition should be created as well with the Invoke method.
The working solution looks like this:
classdef TestMotorization < matlab.mock.TestCase
properties
myMotor
end
methods (TestClassSetup)
function creatMocks(testCase)
import matlab.mock.actions.Invoke
import matlab.mock.actions.StoreValue
import matlab.mock.actions.AssignOutputs;
[motorMock, motorBehaviour] = createMock(testCase, ?handle, ...
'AddedMethods',{'moveToPosition','getCurrentPosition'},'AddedProperties',{'currentAngle'});
% Setup behaviour
when(set(motorBehaviour.currentAngle),StoreValue)
when(withAnyInputs(motorBehaviour.moveToPosition),Invoke(@setValue))
when(withAnyInputs(motorBehaviour.getCurrentPosition),Invoke(@getValue))
% Keep as TestCase property
testCase.myMotor = motorMock;
function setValue(obj, newAngle)
positioningOffset = 0.01;
obj.currentAngle = newAngle + positioningOffset;
end
function angle = getValue(obj)
angle = obj.currentAngle;
end
end
end
methods (Test)
function testMoveMotorizationSuccess(testCase)
testCase.myMotor.moveToPosition(2.1);
currentPosition = testCase.myMotor.getCurrentPosition();
testCase.verifyEqual(currentPosition, 2.11);
end
end
end