Unit test of VcPlugin issue #260
-
Hello, dear all. I have a simple TS function: /**
* Checks if a virtual machine with the given name already exists.
*
* @param {string} vmName - The name of the virtual machine to check.
* @returns {string} - An error message if the virtual machine exists, or undefined if it does not.
*/
(function validateVM(vmName: string) {
const vms: VcVirtualMachine[] = VcPlugin.getAllVirtualMachines(
null,
`xpath:name[matches(.,'${vmName}')]`
);
if (vms.length > 0) {
return `Virtual Machine with that name is already exists`;
}
}); Now, I want to test it. That is the last iteration of trying to mock it. Non one of them didn't work. const validateVM = System.getModule("test2.test2.example").validateVM;
describe("validateVM", () => {
let vm: VcVirtualMachine;
it("should return an error message if a VM with the same name already exists", () => {
spyOn(VcPlugin, "getAllVirtualMachines").and.returnValue([vm]);
const result = validateVM("existingVMName");
expect(result).toEqual("Virtual Machine with that name is already exists");
});
it("should return undefined if no VM with the same name exists", () => {
spyOn(VcPlugin, "getAllVirtualMachines").and.returnValue([]);
const result = validateVM("uniqueVMName");
expect(result).toBeUndefined();
});
}); So, no matter what I am doing, I am always gettin that:
Any ideas now to mock / test VcPlugin or any other similar class? |
Beta Was this translation helpful? Give feedback.
Answered by
unbreakabl3
Apr 10, 2024
Replies: 1 comment 2 replies
-
Found the solution. Thanks. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sure. My bad :)
So, the solution is to add the following into the
describe
sectionBut I am still trying to understand how / why exactly it's doing the job.
@Michaelpalacce , maybe you know? Because I don't see this problem with other classes for example. This was a first time. What is especial in
VcPlugin
?