Skip to content

Instantly share code, notes, and snippets.

@fatso83
Last active January 26, 2023 10:54
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fatso83/329da40002d45533b1e74db825f2bf54 to your computer and use it in GitHub Desktop.
Save fatso83/329da40002d45533b1e74db825f2bf54 to your computer and use it in GitHub Desktop.
Demonstrates how to stub and test for collaborators in a class constructor using sinon and proxyquire. See https://github.com/sinonjs/sinon/issues/831
module.exports = function(n){
// do something with n
}
var Collab = require('./collaborator');
function MyClass(n){
this.collabs = [];
while(n--) this.collabs.push(new Collab(n));
}
module.exports = MyClass;
var assert = require('assert');
var sinon = require('sinon');
var proxyquire = require('proxyquire');
var stub = sinon.stub();
var MyClass = proxyquire('./my-class', { './collaborator' : stub });
var myClass = new MyClass(4);
assert.equal(stub.callCount,4);
assert.equal(stub.callCount,4);
assert(stub.firstCall.calledWith(3));
assert(stub.secondCall.calledWith(2));
assert(stub.getCall(2).calledWith(1));
assert(stub.getCall(3).calledWith(0));
assert(stub.getCall(3).calledWithNew());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment