创建一个模拟与定义的行为在不同的后续方法调用
5视图(30天)
显示旧的评论
假设一个嘲笑类
testCase = matlab.mock.TestCase.forInteractiveUse;
(模拟、行为)= createMock (testCase,“AddedMethods”,(“methodA”“methodB”]);
我如何定义下列行为?
断言(模拟。methodA = = 1)
断言(模拟。methodB = = 2)
断言(模拟。methodA = = 3)
我怎么分配输出值的不同方法模拟一个预定义的顺序吗?我只找到例子定义的行为的后续调用相同的方法,但是没有定义的后续调用的例子包括嘲笑类的不同的方法。类似的
testCase.assignOutputsWhen (withExactInputs (behavior.methodA), 1);
testCase.assignOutputsWhen (withExactInputs (behavior.methodB), 2);
testCase.assignOutputsWhen (withExactInputs (behavior.methodA), 3);
不工作,不幸的是。使用当()/()它允许定义后续行动并不奏效,因为使用的条件是在一个单一的方法,而不是类的几种方法。还有其他可能性吗?
0评论
答案(1)
大卫·赫鲁斯卡对此
2019年12月27日
函数例子
进口matlab.mock.actions.Invoke;
%嵌套函数在方法调用之间跟踪状态
数= 0;
函数=计数器(变长度输入宗量)
数=计数+ 1;
=计数;
结束
testCase = matlab.mock.TestCase.forInteractiveUse;
(模拟、行为)= createMock (testCase,“AddedMethods”,(“methodA”“methodB”]);
%以上这两个方法调用调用计数器功能:
当(withExactInputs (behavior.methodA),调用(@counter));
当(withExactInputs (behavior.methodB),调用(@counter));
断言(模拟。methodA = = 1)
断言(模拟。methodB = = 2)
断言(模拟。methodA = = 3)
结束