javascript - How to use Jasmine spies on an object created inside another method? -


given following code snippet, how create jasmine spyon test confirm dosomething gets called when run myfunction?

function myfunction() {     var foo = new mycoolobject();     foo.dosomething(); }; 

here's test looks like. unfortunately, error when spyon call evaluated:

describe("myfunction", function () {     it("calls dosomething", function () {          spyon(mycoolobject, "dosomething");         myfunction();         expect(mycoolobject.dosomething).tohavebeencalled();      }); }); 

jasmine doesn't appear recognize dosomething method @ point. suggestions?

when call new mycoolobject() invoke mycoolobject function , new object related prototype. means when spyon(mycoolobject, "dosomething") you're not setting spy on object returned new call, on possible dosomething function on mycoolobject function itself.

you should able like:

it("calls dosomething", function() {   var originalconstructor = mycoolobject,       spiedobj;   spyon(window, 'mycoolobject').and.callfake(function() {     spiedobj = new originalconstructor();     spyon(spiedobj, 'dosomething');     return spiedobj;   });   myfunction();   expect(spiedobj.dosomething).tohavebeencalled(); }); 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -