Skip to content

Commit

Permalink
fix(SpyConstructor): correctly forward constructor arguments to the o…
Browse files Browse the repository at this point in the history
…riginal constructor
  • Loading branch information
lukastaegert committed Jan 5, 2017
1 parent ad97a5e commit 198e77d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var R = require('ramda')
var fa = require('fluent-arguments')

function getConstructorInstanceWithArgs (Constructor, constructorArgs) {
return new (Function.prototype.bind.apply(Constructor, arguments))()
return new (Function.prototype.bind.apply(Constructor, [ null ].concat(Array.prototype.slice.call(constructorArgs))))()
}

function getArrayFromArrayLikeObject (args) {
Expand Down
6 changes: 6 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ beforeEach(function () {
})

TestConstructor = function () {
this.args = Array.prototype.slice.call(arguments)
this.field3 = function () { return 3 }
this.field4 = sinon.stub()
Object.defineProperty(this, 'getter', { get: function () { throw new Error('getter was evaluated') } })
Expand Down Expand Up @@ -133,6 +134,11 @@ describe('getSpyConstructor', function () {
expect(spiedObject).to.be.an.instanceof(TestConstructor)
})

it('should create instances using the right arguments', function () {
var spiedObject = new SpyConstructor('Var 1', 2)
expect(spiedObject.args).to.deep.equal([ 'Var 1', 2 ])
})

it('should put spies on all methods', function () {
var spiedObject = new SpyConstructor()

Expand Down

0 comments on commit 198e77d

Please sign in to comment.