How do I mock the matlab system command

조회 수: 7 (최근 30일)
Justin Fitzpatrick
Justin Fitzpatrick 2021년 7월 30일
댓글: Rodrigo 2025년 2월 28일
I am writing some utilities for developers at my organization. One of these utilities interacts directly with the Azure CLI on the user's system to maniuplate data on our Azure DevOps instance. This is done using the system command. As part of the process for developing these utilities, I want to unit test them. I, however, do not want to actually call the system command and Azure CLI; I want to mock that function. It does not appear that there is any way to mock just a function though. I tried creating a mock for system and it errors out:
This is the function I want to Unit Test:
function obj = GetListOfExistingProjects(obj)
cmdstr = "powershell az repos list --organization=https://dev.azure.com/MyOrganization/ --project 'MyProject' ";
[status, cmdout] = system(cmdstr); % <-- this is what I want to mock
data = jsondecode(cmdout);
repos = {};
urls = {};
for repo = 1:length(data)
repos(repo) = {data(repo).name};
urls(repo) = {data(repo).sshUrl};
end
obj.ExistingProjects = repos;
obj.RepoListURLs = urls;
end
This is my test function:
classdef AzureProjectInterfaceTests < matlab.mock.TestCase
methods(Test)
function testGetListOfExistingProjects(testCase)
[mock, behavior] = testCase.createMock(?system);
when(behavior.system("powershell az repos list --organization=https://dev.azure.com/MyOrganization/ --project 'MyProject' "),AssignOutputs('mock response'));
end
end
end
This is the error I get:
Error occurred in tests.AzureProjectInterfaceTests/testGetListOfExistingProjects and it did not run to completion.
---------
Error ID:
---------
'MATLAB:mock:MockContext:ClassNotFound'
--------------
Error Details:
--------------
Error using matlab.mock.internal.MockContext (line 192)
The value of 'Superclass' is invalid. Unable to find the specified
class.
Error in matlab.mock.TestCase/createMock (line 179)
context = MockContext(testCase, varargin{:});
Error in
tests.AzureProjectInterfaceTests/testGetListOfExistingProjects (line
6)
[mock, behavior] = testCase.createMock(?system);
Does anyone know a way to successfully mock a matlab built-in function?
  댓글 수: 2
Justin Fitzpatrick
Justin Fitzpatrick 2021년 7월 30일
Additional Information: I already read this: How to create a unit testing mocking framework for function files? - MATLAB Answers - MATLAB Central (mathworks.com) which seems to work for custom functions without a class, but I do not think this will work for a built-in matlab function.
Rodrigo
Rodrigo 2025년 2월 28일
did find an answer? I'm trying to mock "dir" and I don't seem to be able to do it :(

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Mock Dependencies in Tests에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by