MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Create a default FileChooser that uses the uigetfile function for file selection.


classdef DefaultFileChooser < FileChooser
methods
function [file,folder,status] = chooseFile(chooser,varargin)
[file,folder,status] = uigetfile(varargin{:});
end
end
end


Change the app to accept an optional FileChooser object. When called with no inputs,
the app uses an instance of DefaultFileChooser.


function app = launchApp(fileChooser)
if nargin==0
fileChooser = DefaultFileChooser;
end
f = uifigure;
button = uibutton(f,'Text','Input file');
button.ButtonPushedFcn = @(src,evt)pickFile(fileChooser);
label = uilabel(f,'Text','No file selected');
label.Position(1) = button.Position(1) + button.Position(3) + 25;
label.Position(3) = 200;


% Add components to an App struct for output
app.UIFigure = f;
app.Button = button;
app.Label = label;


function file = pickFile(fileChooser)
[file,folder,status] = fileChooser.chooseFile('.');
if status
label.Text = file;
end
end
end


Make the following modifications to LaunchAppTest.



  • Change the test to inherit from both matlab.uitest.TestCase and
    matlab.mock.TestCase.

  • Remove the properties block and the TestClassSetup block. Because the mock
    defines the output of the chooseFile method call, the test does not rely on the
    existence of an external file.


Write Test That Uses App Testing and Mocking Frameworks
Free download pdf