javascript - Why is an identical Mocha test failing after the first one? -


i have class myclass accepts objects attach methods. these methods call myclass.push add data object's internal @_list array. second of 2 identical test cases fails, don't understand why:

chai = require "chai" chai.should()  obj =   name: 'foo'   f: ->     in [1..5]       @push "element #{i}"   class myclass   constructor: (@options) ->     @methods = {}     @_list = []      if 'attach' of @options       @attach @options.attach    push: (m) ->     @_list.push m    list: ->     @_list    method: (v) ->     @methods[v].f    run: (options) ->     @method('foo')()    attach: (o) ->     @methods[o.name] = o     @methods[o.name].f = o.f.bind(@)  describe 'myclass', ->   'list should have 5 elements', ->     v = new myclass attach: obj     v.run()     v.list().length.should.equal 5   'list should have 5 elements (#2)', ->     v = new myclass attach: obj     v.run()     v.list().length.should.equal 5 

i believe has function binding going, because when replace second v = new myclass attach: obj with:

v = new myclass attach:   name: 'foo'   f: ->     in [1..5]       @push "element #{i}" 

both tests pass. don't understand behavior, thought .bind() creates copy of function. what's going on here, , how both tests run independently?

javascript (and coffeescript) objects pass reference . when start second test, obj object has changed since you've modified f attribute doing:

@methods[o.name].f = o.f.bind(@)

you can pass copy of instead avoid this.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -