MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

  • Change the testInputButton test method so that it will do these things.

    • Create a mock object of the FileChooser.

    • Define mock behavior such that when the chooseFile method is called with the
      input '.', the outputs are the test file name ('input2.txt'), the current
      working folder, and a selected filter index of 1. These outputs are analogous to the
      outputs from the uigetfile function.

    • Press the button and verify the selected file name. These steps are the same as in
      the original test, but the mock assigns the output values, so you do not need to
      interact with the app to continue testing.



  • To test the Cancel button, add a test method testInputButton_Cancel so that it
    will do these things.

    • Create a mock object of the FileChooser.

    • Define mock behavior such that when the chooseFile method is called with the
      input '.', the outputs are the test file name ('input2.txt'), the current
      working folder, and a selected filter index of 0. These outputs are analogous to the
      outputs from the uigetfile function if a user selects a file and then chooses to
      cancel.

    • Press the button and verify that the test calls the chooseFile method and that the
      label indicates that no file was selected.
      classdef LaunchAppTest < matlab.uitest.TestCase & matlab.mock.TestCase
      methods (Test)
      function testInputButton(tc)
      import matlab.mock.actions.AssignOutputs
      fname = 'myFile.txt';
      [mockChooser,behavior] = tc.createMock(?FileChooser);
      when(behavior.chooseFile('.'),AssignOutputs(fname,pwd,1))
      app = launchApp(mockChooser);
      tc.addTeardown(@close,app.UIFigure);
      tc.press(app.Button);
      tc.verifyEqual(app.Label.Text,fname);
      end
      function testInputButton_Cancel(tc)
      import matlab.mock.actions.AssignOutputs
      [mockChooser, behavior] = tc.createMock(?FileChooser);
      when(behavior.chooseFile('.'),AssignOutputs('myFile.txt',pwd,0))
      app = launchApp(mockChooser);
      tc.addTeardown(@close,app.UIFigure);
      tc.press(app.Button);




33 Unit Testing

Free download pdf