First time trying to use Jasmine spies so I hope I’m just missing something obvious. What I want to do is track calls to a function that I have defined as:
window.myFunction = ->
I have a class method that calls this function. The method works fine, and I can test most aspects of it, but the following fails:
beforeEach ->
spyOn(window, 'myFunction').andCallThrough()
it 'should do that thing', ->
MyClass.makesCallToMyFunction
expect(window.myFunction).toHaveBeenCalled()
What am I doing wrong? I’ve seen plenty of examples on SO and many of them use the spyOn(window, ‘myFunction’)…expect(window.myFunction) setup/spec.
Any insight is appreciated! Thanks.
This will fail:
because
MyClass.makesCallToMyFunctionis not a method call, that’s simply a reference to themakesCallToMyFunctionfunction. If you want to call a CoffeeScript function/method without any arguments then you need to include the parentheses or CoffeeScript won’t know that you want to call the function: