Matlab:如何对Questdlg盒进行单元测试?

时间:2017-10-25 19:58:37

标签: matlab unit-testing messagebox

我有一大堆代码显示一个有三个选项的问题框:

  • 另存为
  • 没有

我只是想为每个选项创建一个单元测试,我需要与它进行交互。 创建框后,每个matlab进程都会被冻结,直到用户做出反应。当然,我的测试是自动化的,并且没有用户在场。

是否有解决方案发送"是"事件到Matlab? 我可以使用autoit但我宁愿避免使用

由于

1 个答案:

答案 0 :(得分:1)

目前,您最好的选择是将代码结构化为不依赖于questdlg函数,而是围绕函数包装一个接口,以便您可以注入特定于测试的依赖项,或者" mock&# 34 ;.这可能类似于以下内容:

待测源代码

function codeWhichUsesQuestDlg(dlgProvider)
validateattributes(dlgProvider, {'DialogProvider'},{});

if (needToAskQuestion)
    result = dlgProvider.provideQuestionDialog(...)
else
    ...
end

源代码接口/基础架构

classdef DialogProvider < handle
    methods(Abstract)
        result = provideQuestionDialog(varargin);
        % perhaps others? errordlg, etc?
    end
end

生产实施

classdef ProductionProvider < DialogProvider
    methods
        function result = provideQuestionDialog(varargin)
            result = questdlg(varargin{:}); 
        end
    end
end

生产用途

>> codeWhichUsesQuestDlg(ProductionProvider)

测试用法

classdef TestCode < matlab.mock.TestCase

    methods(Test)
        function testCode(testCase)
            % Create the test specific "mock" implementation
            [mockProvider, behavior] = testCase.createMock(?DialogProvider);

            % Define mock behavior needed for the test
            when(withAnyInputs(behavior.provideQuestionDialog), ...
                AssignOutputs('Yes'));

            % Call code under test with the mock
            codeWhichUsesQuestDlg(mockProvider);

            % Verify correct result/behavior
            ...
        end
    end
end

查看mocking framework以查看更多信息。您也可能对this post介绍模拟框架以及依赖注入的this post感兴趣。

相关问题