Last active
January 26, 2023 10:54
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(n){ | |
// do something with n | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Collab = require('./collaborator'); | |
function MyClass(n){ | |
this.collabs = []; | |
while(n--) this.collabs.push(new Collab(n)); | |
} | |
module.exports = MyClass; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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