Angular jasmine spyon example


Jasmine spyon is a double functions to test methods and object of an class in Javascript and typescript.

It is basically allows you call methods or functions and returns the result.

Syntax

spyOn(object,"function").withArgs(arguments).and.returnValue(value);

Calling a method from constructor of an class

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(); }); });

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.