Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

Christian Niklaus
Christian Niklaus el 14 de Sept. de 2023
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

Más respuestas (0)

Categorías

Más información sobre Mocking Framework en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by