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.
How do I mock the matlab system command
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Rodrigo
am 28 Feb. 2025
did find an answer? I'm trying to mock "dir" and I don't seem to be able to do it :(
Antworten (0)
Siehe auch
Kategorien
Mehr zu Mock Dependencies in Tests finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!