cjohansen/Sinon.JS — pull request #275 by micha149 (2013-04-26 06:01:20+00:00)
Title: Implemented stubbing of constructors

On projects with the dependency injection pattern, it may be very useful to be able to stub complete prototypes of constructors. In this cases we need to inject an instance of the required constructor with all its prototype functions, but not to execute its constructor.

Usage example:

test('calling log fomats the input and passes it to writer', function() {
var writer = sinon.stub(Example.Logger.Writer),
formatter = sinon.stub(Example.Logger.Formatter),
logger = new Example.Logger(writer, formatter);

formatter.format.returns('formatted text');

logger.log('test text');

assert.ok(formatter.format.calledOnce);
assert.equal(formatter.format.firstCall.args[0], 'test text');
assert.ok(writer.write.calledOnce);
assert.equal(writer.write.firstCall.args[0], 'formatted text');
})