Jasmine + test of the inherited method was called

What is the best way to verify with Jasmine that the legacy method has been called?

I’m only interested in checking if it was called, since I have installed unit tests for the base class.

example:

YUI().use('node', function (Y) { function ObjectOne () { } ObjectOne.prototype.methodOne = function () { console.log("parent method"); } function ObjectTwo () { ObjectTwo.superclass.constructor.apply(this, arguments); } Y.extend(ObjectTwo, ObjectOne); ObjectTwo.prototype.methodOne = function () { console.log("child method"); ObjectTwo.superclass.methodOne.apply(this, arguments); } }) 

I want to verify that the ObjectTwo inherited method One has been called.

Thanks in advance.

+6
source share
1 answer

To do this, you can use the method in the ObjectOne prototype.

 spyOn(ObjectOne.prototype, "methodOne").andCallThrough(); obj.methodOne(); expect(ObjectOne.prototype.methodOne).toHaveBeenCalled(); 

The only caveat to this method is that it will not check if methodOne was called in the obj object. If you need to make sure that it was called on an obj object, you can do this:

 var obj = new ObjectTwo(); var callCount = 0; // We add a spy to check the "this" value of the call. // // This is the only way to know if it was called on "obj" // spyOn(ObjectOne.prototype, "methodOne").andCallFake(function () { if (this == obj) callCount++; // We call the function we are spying once we are done // ObjectOne.prototype.methodOne.originalValue.apply(this, arguments); }); // This will increment the callCount. // obj.methodOne(); expect(callCount).toBe(1); // This won't increment the callCount since "this" will be equal to "obj2". // var obj2 = new ObjectTwo(); obj2.methodOne(); expect(callCount).toBe(1); 
+3
source

All Articles