cjohansen/Sinon.JS — pull request #480 by JamesBarwell (2014-05-13 18:04:47+00:00)
Title: Fix spy createCallProperties not created early enough

Hi, I found a possible issue with createCallProperties not being executed in time, leading to firstCall, secondCall, lastCall, etc not being populated. This is in the context of Mocha's asynchronous beforeEach function, where spying on 'done' or a function that calls 'done'. After the spy invokes the method, control continues within the test runner and assertions are made, and only afterwards does the spy's createCallProperties function run.

I've included a failing test for firstCall and a possible fix.

Here's a brief code example (simplified from my real one, which does require the async beforeEach) that shows the real-world situation in which this is occurring.

```js
var mocha = require('mocha');
var sinon = require('sinon');

describe('createCallProperties', function() {
var callback;

beforeEach(function(done) {
function foo() {
done();
}
callback = sinon.spy(foo);
callback();
});

it('should set lastCall and getCall(0) to be the same', function() {
console.log(callback.callCount); // returns 1
console.log(callback.lastCall); // returns null
console.log(callback.getCall(0)); // returns call object
});
});
```