Create a mock with behavior based on specific input
Ältere Kommentare anzeigen
I'm trying to write a mock test for a class, and want to mock the output based on specific input.
Here is my Test Class
classdef MyClass
methods
function [out1, out2] = myMethod(obj, in1, in2, in3)
out1 = in1 + in2 + in3;
out2 = in1 * in2 * in3;
end
end
end
Based on MATLAB help section withExactInput and withAnyInputs doesnt take any inputs, Attempting to do so raises the error

So how to mock a test where same methods needs to be called with 2 differnet inputs and check the outputs.
classdef MyTest < matlab.mock.TestCase
methods (Test)
function testMultiInputOutputMethod(testCase)
% Create a mock object and its behavior controller
[mock, behavior] = testCase.createMock(?MyClass);
% Define the behavior of the mock object's method for specific inputs
testCase.assignOutputsWhen(withExactInput(behavior.myMethod(1, 2, 3)), {6, 6}); % Errors out
testCase.assignOutputsWhen(withAnyInputs(behavior.myMethod(2, 3, 4)), {9, 24}); % Errors out
% Use the mock object as if it were the real object
[out1a, out2a] = mock.myMethod(1, 2, 3);
[out1b, out2b] = mock.myMethod(2, 3, 4);
% Verify the results
testCase.verifyEqual(out1a, 6);
testCase.verifyEqual(out2a, 6);
testCase.verifyEqual(out1b, 9);
testCase.verifyEqual(out2b, 24);
% Verify that the mock object's methods were called with specific arguments
testCase.verifyCalled(behavior.myMethod(1, 2, 3));
testCase.verifyCalled(behavior.myMethod(2, 3, 4));
end
end
end
I know the above 'MyClass' doesnt need a mock, Any solution to the above problem would be helpful...
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Mock Dependencies in Tests finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!