Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to spy on nested method in Jasmine?

Consider this usual situation:

var a = {
  b: {
    c: function() {}
  }
}

I want to spy on c and though it's easy with:

spyOn(a.b, 'c');

However it creates a spy but it doesn't work. No errors or so are shown and I can see there's a spy when debugging.

How can I spy on a nested method?

UPDATE

Output is: Object [object Object] has no method 'tohaveBeenCalledWith'

like image 501
lukas.pukenis Avatar asked Jul 12 '13 14:07

lukas.pukenis


1 Answers

I guess typo is the problem; spying on nested functions works well as you outlined.

Be carefull with the casing: Jasmine function is toHaveBeenCalled(). Since you wrote tohaveBeenCalled() the error message makes sense (because there is no such method). JavaScript is case-sensitive :-)

like image 80
zbynour Avatar answered Sep 17 '22 13:09

zbynour