I am using the following construct as a Mock object which functions as an object with chainable methods:
PlayerNull =
find : ->
populate : ->
exec : (callback) ->
callback false, false
In my tests I then substitute the real model for this Mock object and my controller calls each of the functions in turn such as:
Model.find().populate().exec(callback)
As I'm finding myself using this many times, I was curious as to whether I could create a helper function to simplify this (slightly), using a helper function in the following form:
PlayerNull = helper.mockNest ['find', 'populate', 'exec'], (callback) ->
callback false, false
I've come to the following function code, however this is not working:
exports.mockNest = (func_names, func_final) ->
func_names.reverse()
func_next = func_final
for func_name in func_names
_func_next = func_next.bind({})
_next = {}
_next[func_name] = ->
_func_next
func_next = _func_next
func_next
I've come to the realization that I need to clone func_next
each loop or else the reference seems to be maintained and assigning to func_next
just seems to alter all previous assignments.