Testclass: How to store a method input within a mocked class property?

4 次查看(过去 30 天)
I want to mock an physical instrument with a motorized wheel within my testclass. My goal is to mock the two main functionalities:
  • moveToPosition(obj, targetAngle) triggers the controller to move the wheel to the desired position
  • currentAngle = getCurrentPosition(obj) read out the encoder to get the current wheel position
As no hardware is attached, the mocked moveToPosition method shall only store the target angle as property such that the getCurrentPosition method can return this value toghether with a small offset.
I wrote the following example. It failes because matlab.mock.TestCase seems to be a value class and not a handle class. Therefore the property currentAngle is not assigned permanently within setValue:
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,...
'AddedMethods',{'moveToPosition','getCurrentPosition'},'AddedProperties',{'currentAngle'});
% Setup behaviour
when(set(motorBehaviour.currentAngle),StoreValue)
when(withAnyInputs(motorBehaviour.moveToPosition),Invoke(@setValue))
when(withAnyInputs(motorBehaviour.getCurrentPosition),AssignOutputs(motorMock.currentAngle))
% Keep as TestCase property
testCase.myMotor = motorMock;
function setValue(obj, newAngle)
positioningOffset = 0.01;
obj.currentAngle = newAngle + positioningOffset;
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
Is there a way to store a method input within a mock class property and read it out later with another method?

采纳的回答

Christian Niklaus
Christian Niklaus 2023-9-14
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mock Dependencies in Tests 的更多信息

产品


版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by