javascript - Jasmine: how to spy on inner object method call? -


i have 2 prototypes want test:

var person = function() {};  person.prototype.pingchild = function(){    var boy = new child();    boy.getage(); }  var child = function() {};  child.prototype.getage = function() {     return 42; }; 

what want test: to check getage() method called inside of pingchild() method

that jasmine specs try use purpose:

describe("person", function() {     it("calls getage() function", function() {         var fakeperson = new person();         var chi = new child();         spyon(fakeperson, "getage");         fakeperson.pingchild();         expect(chi.getage).tohavebeencalled();     }); });  describe("person", function() {     it("calls getage() function", function() {         var fakeperson = new person();         spyon(fakeperson, "getage");         fakeperson.pingchild();         expect(fakeperson.getage).tohavebeencalled();     }); });  describe("person", function() {     it("calls getage() function", function() {         var fakeperson = new person();         var chi = new child();         spyon(chi, "getage");         fakeperson.pingchild();         expect(chi.getage).tohavebeencalled();     }); }); 

but of them shows errors:

  • getage() method not exist
  • getage() method not exist
  • expected spy getage have been called

so, there way test such cases using jasmine, , if yes - how can done?

you have yo spy on prototype of child object.

describe("person", function () {   it("calls getage() function", function () {     var spy = spyon(child.prototype, "getage");     var fakeperson = new person();     fakeperson.pingchild();     expect(spy).tohavebeencalled();   }); }); 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -