Testclass: How to store a method input within a mocked class property?
古いコメントを表示
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?
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Mock Dependencies in Tests についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!