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

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2314 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.