Jasmine Spies Not Working

jasmine, javascript

Solution

The documented way to get the call count for a spy is via the `calls` property:

myCtrFn.myMethod.calls.count() // 1

Documentation: http://jasmine.github.io/2.0/introduction.html#section-23

Looking at the sources, it seems this information is not available anywhere else: https://github.com/pivotal/jasmine/blob/master/src/core/CallTracker.js https://github.com/pivotal/jasmine/blob/master/src/core/base.js#L75

Problem

Given the following code (with Jasmine included in the page): ``` function MyCtorFn() { this.myMethod = function() { console.log("hello world") } } //arrange var myCtrFn = new MyCtorFn(); spyOn(myCtrFn, 'myMethod'); //act myCtrFn.myMethod(); ``` Why does the following return undefined? ``` myCtrFn.myMethod.callCount ```

Original source