TDD – Jasmine Test CoffeeScript (Settimeout) .tohaveBeenCalledwith

In the process of learning Jasmine, I encountered this problem. I want a basic function to run, and then set a timeout to call myself again… simple things.

class @LoopObj
constructor: ->
loop: (interval) ->
#do some stuff
setTimeout( (=>@loop(interval)), interval)

But I want to test to make sure that setTimeout is called with the correct args

describe "loop", ->
xit "does nifty things", ->
it "loops at a given interval", ->
my_nifty_loop = new LoopObj
interval = 10
spyOn (window, "setTimeout")
my_nifty_loop.loop(interval)
expect(setTimeout).toHaveBeenCalledWith((-> my_nifty_loop.loop(interval)), interval)

I get This error: The expected spy setTimeout is called with [Function,10] but is called with [[Function,10]]

This is because the function (-> my_nifty_loop.loop(interval)) is not equal to ( => @loop(interval)) function? Or is it related to the extra square brackets around the second [[function,10]]? What else altogther?

Where did I go wrong?

I don’t know much about CoffeeScript, but you can debug it by replacing

expect(setTimeout).toHaveBeenCalledWith((-> my_nifty_loop.loop(interval)), interval)

Same

< pre>expect(setTimeout).toHaveBeenCalledWith(jasmine.any(Function), interval)

and re-run the specification. I think if the extra square brackets disappear, then your problem is because you have two Different function references. If they don’t disappear, your LoopObj definition will have something weird, maybe a fat arrow operator (it looks like my n00b eyes don’t need it).

In the process of learning Jasmine, I encountered this problem. I want a basic function to run, and then set a timeout to call myself again… simple stuff.

< /p>

class @LoopObj
constructor: ->
loop: (interval) ->
#do some stuff
setTimeout((=>@loop(interval) ), interval)

But I want to test to make sure that setTimeout is called with the correct args

describe "loop", ->
xit " does nifty things", ->
it "loops at a given interval", ->
my_nifty_loop = new LoopObj
interval = 10
spyOn(window, "setTimeout")< br /> my_nifty_loop.loop(interval)
expect(setTimeout).toHaveBeenCalledWith((- > my_nifty_loop.loop(interval)), interval)

I get this error: The expected spy setTimeout is called with [Function,10] but is called with [[Function,10]]

This is because the (-> my_nifty_loop.loop(interval)) function is not equal to the (=> @loop(interval)) function? Or is it related to the extra square brackets around the second [[function,10]]? What else altogther?

Where did I go wrong?

I don’t know much about CoffeeScript, but you can debug it by replacing

expect( setTimeout).toHaveBeenCalledWith((-> my_nifty_loop.loop(interval)), interval)

Same

expect(setTimeout).toHaveBeenCalledWith(jasmine.any (Function), interval)

and re-run the specification. I think if the extra square brackets disappear, then your problem is because you have two different function references. If they don’t disappear, your The LoopObj definition will have some strange things, it may be a fat arrow operator (it seems that my n00b eyes don’t need it).

Leave a Comment

Your email address will not be published.