admin管理员组

文章数量:1291097

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'

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'

Share Improve this question edited Jul 12, 2013 at 14:52 lukas.pukenis asked Jul 12, 2013 at 14:38 lukas.pukenislukas.pukenis 13.6k13 gold badges49 silver badges82 bronze badges 2
  • Can explain what does not work, cause this is the right way to do it. – Andreas Köberle Commented Jul 12, 2013 at 14:40
  • @AndreasKöberle when I do except(a.b.c).haveBeenCalled() I get Object [object Object] has no method 'tohaveBeenCalled' – lukas.pukenis Commented Jul 12, 2013 at 14:51
Add a ment  | 

1 Answer 1

Reset to default 7

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 :-)

本文标签: javascriptHow to spy on nested method in JasmineStack Overflow