4

Let say we have a function that returns a function and bounds arguments to it:

function A(x, y){
    return function(x, y){...}.bind(this, x, y);
}

And now we want to know if function A binds arguments correctly:

var resultedFunction = A();
var spy = sinon.spy(resultedFunction);
spy();

The question - is it possible to know if arguments are bound correctly? I've tried this, but it is an empty array

spy.firstCall.args
spy.getCall(0).args
Arsey
  • 281
  • 3
  • 10
  • I believe it would be fine to test if `A` was called with correct arguments. You probably don't want to test `bind` after all. – Роман Парадеев May 09 '16 at 19:50
  • @РоманПарадеев, when we test A, it is its responsibility to bind arguments for the function it returns. Isn't it? I think that your suggestion would be more acceptable if we would test some function B that calls A with arguments. – Arsey May 09 '16 at 20:01
  • Binding in your example is an implementation detail. You could achive identical behaviuor for `A` using a closure, and the test should not begin failing, since the function remains correct. – Роман Парадеев May 09 '16 at 20:10

1 Answers1

1

I've finally come to some trick. If the returning function will be not an anonymous then we can spy it and check arguments later:

var obj = {
  B: function(){
    ...
  },
  A: function(x, y){
    return this.B.bind(this, x, y);
  }
}
var x = 1, y = 2;
var spy = sinon.spy(obj, "B");
var resultedFunction = obj.A(x, y);

resultedFunction();

expect(spy.firstCall.args[0]).to.equals(x)
expect(spy.firstCall.args[0]).to.equals(y)
Arsey
  • 281
  • 3
  • 10