Angular unit test Calling a method from the constructor of a class
- Admin
- Dec 31, 2023
- Angular-test
Jasmine spyon is a double function to test methods and objects of a class in Javascript and typescript.
It allows you to call methods or functions and returns the result.
Syntax
spyOn(object, "function").withArgs(arguments).and.returnValue(value);
Angular unit test Calling a method from the constructor of a class
Here is an example of calling a method from a construction
myclass = function () {
this.mymethod();
};
myclass.prototype.mymethod = function () {
console.log("mymethod");
};
describe("The myclass constructor", function () {
it("should call its prototype's mymethod", function () {
spyOn(myclass.prototype, "mymethod"); //.andCallThrough();
var myc = new myclass();
expect(myclass.prototype.mymethod).toHaveBeenCalled();
});
});