Assign value to property of mock object when method is called
56 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ralf Ritter
el 25 de Nov. de 2020
Comentada: Ralf Ritter
el 11 de Dic. de 2020
Hi, is there a way to assign a value to a property of a mock object when a method of the same object is called?
Let's say I create the following mock:
[mock, behavior] = createMock('AddedMethods', "doSomething", 'AddedProperties', "propA");
Now I would like to do something like:
when(withAnyInputs(behavior.doSomething), set(mock.propA, true));
Obviously, that's no valid code, but I think you get what I mean. Thanks for your help!
2 comentarios
Houman Rastegarfar
el 2 de Dic. de 2020
Hi Ralf,
You can set the mock object property to the value returned by the mock object method. To achieve this, specify separate behaviors for the method and the property. You can specify one behavior at a time.
import matlab.mock.actions.*
testCase = matlab.mock.TestCase.forInteractiveUse;
[mock,behavior] = testCase.createMock('AddedMethods',"doSomething",'AddedProperties',"propA");
%Set up behavior when the method is called
when(withAnyInputs(behavior.doSomething),AssignOutputs(true))
%Set up behavior when the property is set
when(set(behavior.propA),StoreValue)
% Invoke the method and set the property
mock.propA = mock.doSomething
-Houman
Respuesta aceptada
David Hruska
el 10 de Dic. de 2020
function example
import matlab.mock.actions.Invoke;
testCase = matlab.mock.TestCase.forInteractiveUse;
[mock, behavior] = testCase.createMock('AddedMethods', "doSomething", 'AddedProperties', "propA");
when(withAnyInputs(behavior.doSomething), Invoke(@setPropA));
function obj = setPropA(obj)
obj.propA = true;
end
mock = mock.doSomething;
disp(mock);
end
Más respuestas (0)
Ver también
Categorías
Más información sobre Mock Dependencies in Tests en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!