You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Inside some of my tests I've found that I need to spy on instance methods, but it seems that it's impossible at present since these are read only properties. When I try to use sinon's spy method...
varnext_spy=sinon.spy(player,'next');
this happens:
TypeError: Cannot assign to read only property 'next' of #<Object>
at Object.wrapMethod (C:\Users\Timothy\Repos\sync4\node_modules\sinon\lib\sinon.js:84:30)
at Object.spy (C:\Users\Timothy\Repos\sync4\node_modules\sinon\lib\sinon\spy.js:228:22)
at C:\Users\Timothy\Repos\sync4\test\db_room_model.js:102:26
at C:\Users\Timothy\Repos\sync4\node_modules\orm\lib\Model.js:297:12
at C:\Users\Timothy\Repos\sync4\node_modules\orm\lib\Singleton.js:32:10
at Object.<anonymous> (C:\Users\Timothy\Repos\sync4\node_modules\orm\lib\Model.js:112:12)
at C:\Users\Timothy\Repos\sync4\node_modules\orm\lib\Instance.js:24:7
at Array.map (native)
at Instance.emitEvent (C:\Users\Timothy\Repos\sync4\node_modules\orm\lib\Instance.js:23:17)
at C:\Users\Timothy\Repos\sync4\node_modules\orm\lib\Instance.js:608:4
Is there a recommended method for mocking/spying on instance methods?
Room.Player=Room.extendsTo('player',{elapsed: {type: 'number',defaultValue: 0},duration: Number,order: {type: 'enum',values: ['normal','shuffle'],defaultValue: 'normal'}},{// TODO use this instead -> autoFetch: true,hooks: {afterLoad: function(){// TODO see if this can be mixed in to the instanceif(!this.events)this.events=newEventEmitter;this.timer;}},methods: {// select and play the next itemnext: function(cb){cb=cb||function(){};varplayer=this;player.getRoom(function(err,room){if(err)returncb(err);room.getItems(function(err,items){if(err)returncb(err);if(items.length===0){player.load(null,function(err){if(err)returncb(err);returncb();});return;}varitem;if(player.order==='normal')item=items[0];if(player.order==='shuffle')item=items[_.random(items.length)];room.removePlaylistItem(item.id,function(err){if(err)returncb(err);player.load(item,function(err){if(err)returncb(err);returncb();});});});});},
...
},reverse: 'room'});
I'm not sure if the next word is not reserved for iterators. You should change it before it bites you when ES6 arrives (without having to pass options to node executable).
It defines the methods not specifying configurable (so the default is false), meaning it cannot be changed later. This is what sinon is trying to do. Please change it in your orm copy and see if it works. Something like this:
Activity
dxg commentedon Aug 11, 2013
What exactly is
next?takempf commentedon Aug 11, 2013
For what it's worth...
dresende commentedon Aug 12, 2013
I'm not sure if the
nextword is not reserved for iterators. You should change it before it bites you when ES6 arrives (without having to pass options tonodeexecutable).dresende commentedon Aug 12, 2013
I think this is because of this line:
https://github.com/dresende/node-orm2/blob/master/lib/Instance.js#L457
It defines the methods not specifying
configurable(so the default isfalse), meaning it cannot be changed later. This is what sinon is trying to do. Please change it in your orm copy and see if it works. Something like this:If it works I'll update it. I don't want to touch the repository now since I'm waiting for some changes from a fork and don't want to cause conflicts.
dresende commentedon Aug 12, 2013
It might be the
writableoption and not theconfigurableone.dresende commentedon Sep 13, 2013
If you still want this and the above change works please reopen and I'll change it.
takempf commentedon Sep 21, 2013
@dresende the
writablechange fixes the issue for me.Changes user methods to be writeable property instances (fixes #296)