unit testing instanceof VcVirtual*Controller #454
-
Hi all, we have a new piece of code that iterates over the scsi controllers of a VM, to check if the device we're looking at is indeed a scsi controller the programmer has written: const isScsi = controller instanceof VcVirtualBusLogicController ||
controller instanceof VcVirtualLsiLogicController ||
controller instanceof VcParaVirtualSCSIController ||
controller instanceof VcVirtualLsiLogicSASController which is perfectly valid code, but all we get during unit testing is "ReferenceError: VcVirtualBusLogicController is not defined". Is there a way to make these types to be defined? Or should we go about finding the type of the controller differently? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Here is an example how you can define instances that don't exist as they are not part of the scripting api: beforeEach(function () {
(<any> VcVirtualBusLogicController) = {
someMethod: () => {
throw new Error("Not Implemented, add a spy");
}
};
});
afterEach(function () {
(<any>VcVirtualBusLogicController) = undefined;
}) I am not sure how it will apply here, tho. And unit testing this may not even make sense in the first place. There are many downsides to using // SCSIDevice Use this interface moving forward instead of all 4 options or `any`
export interface SCSIDevice {
// Define the interface yourself
}
/**
* Checks if `scsiCtlrUnitNumber` is a function
*
* @TODO: Potentially add more
*
* Ref:
* - https://www.vroapi.com/Class/VC/7.0.0/VcParaVirtualSCSIController
* - https://www.vroapi.com/Class/VC/7.0.0/VcVirtualLsiLogicController
* - https://www.vroapi.com/Class/VC/7.0.0/VcVirtualBusLogicController
* - https://www.vroapi.com/Class/VC/7.0.0/VcVirtualLsiLogicSASController
* - https://www.vroapi.com/Class/VC/7.0.0/VcVirtualDevice
*/
export function isScsi(controller: any): boolean {
return typeof controller.scsiCtlrUnitNumber === 'function' &&
typeof controller.sharedBus === 'function';
} it would also simplify your tests |
Beta Was this translation helpful? Give feedback.
-
@pe1pip closing for now, feel free to re-open if further feedback is needed. |
Beta Was this translation helpful? Give feedback.
Here is an example how you can define instances that don't exist as they are not part of the scripting api:
I am not sure how it will apply here, tho. And unit testing this may not even make sense in the first place.
There are many downsides to using
instanceof
that you can look up if interested. You could try to do something like so: