Mocking a Set Method doesn't work

조회 수: 3 (최근 30일)
aoisdjas alsjdlsajd
aoisdjas alsjdlsajd 2021년 10월 8일
답변: Samayochita 2025년 6월 23일
I want to Mock a set method while conducting unit tests. when I try to mock it, I get the error:
Unrecognized method, property, or field 'set' for class
Any help is appreciated.
The mock is defined as follows:
testCase.assignOutputsWhen(...
withAnyInputs(behaviorObj.set.blablabla),DoNothing);
The Set method is defined as follows:
function set.blablabla(obj)
arguments
end
%"stuf"
end

답변 (1개)

Samayochita
Samayochita 2025년 6월 23일
Hi aoisdjas alsjdlsajd,
I understand that you are trying to mock a set method for a property in MATLAB. MATLAB does not support mocking property set methods directly using “matlab.mock.TestCase.assignOutputsWhen”. In MATLAB, property accessors like get and set are not methods in the same way regular class methods are. The error is expected because “set.blablabla” is not a callable method you can reference like “obj.someMethod”.
I would instead suggest you to wrap the setter logic in a regular method. Define a regular method like “setBlablabla()”:
methods
function setBlablabla(obj, value)
obj.blablabla = value; % raw property set
% Do more stuff here
end
end
Then you can mock that method easily as follows:
testCase.assignOutputsWhen(...
withAnyInputs(behaviorObj.setBlablabla), DoNothing);
Hope this is helpful!

카테고리

Help CenterFile Exchange에서 Mock Dependencies in Tests에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by