sinon-doublist

Sinon.JS test double mixins

sinon-doublist

Sinon.JS test double mixins: spyMany, stubMany, stubWithReturn, stubBind

  • Double multiple methods in one call.
  • Use sinon.testCase-like auto-sandboxing.
  • Optionally use plain objects, even empty ones, to hold method doubles.
  • Select target methods x.y.z property path strings.

sinonDoublist(sinon, 'mocha');
 
describe('myFunction', function() {
  it('should do something', function() {
    // this.spyMany() 
    // this.stubMany() 
    // this.stubWithReturn() 
  });
});
describe('myFunction', function() {
  beforeEach(function() {
    sinonDoublist(sinon, this);
  });
 
  afterEach(function() {
    this.sandbox.restore();
  });
 
  it('should do something', function() {
    // this.spyMany() 
    // this.stubMany() 
    // this.stubWithReturn() 
  });
});

Creates spies for multiple methods, even though the latter do not exist yet.

var spy = this.spyMany({}, ['a.b.methodA', 'c.e.methodB', 'd.e.methodC']);
spy['a.b.methodA'].restore();

Creates a stub for method foo() that returns false only if called with argument 'bar'.

var obj = {};
var foo = this.stubMany(obj, 'foo').foo;
foo.withArgs('bar').returns(false);
foo.restore();

Creates a stub that, if called with argument 'foo', returns object containing a spy at path x.y.z.

var obj = {};
 
stub = this.stubWithReturn({
  obj: obj,
  args: ['foo']
  method: 'methodD',
  spies: 'x.y.z'
});
var spiesReturnedFromStub = obj.methodD();
spiesReturnedFromStub.x.y.z('foo');
spiesReturnedFromStub.x.y.z.called.should.equal(true);
function target() {}
function fakeBoundTarget() {}
 
var stub = this.stubBind(target, null, 1, 2, 3).bind;
stub.bind.returns(fakeBoundTarget);
 
target.bind(null, 3, 2, 1); //  
console.log(stub.bind.called); // false 
 
target.bind(null, 1, 2, 3); // fakeBoundTarget 
console.log(stub.bind.called); // true 

As of 0.5.0, useFakeTimers is no longer enabled by default. sinon now fakes setImmediate in that feature, which may cause confusion if automatically enabled.

To enable:

this.clock = this.sandbox.useFakeTimers();

component

component install codeactual/sinon-doublist

NPM

npm install sinon-doublist

Documentation

MIT

npm test

Browser via Karma

  • npm install karma
  • grunt build && karma start
  • Browse http://localhost:9876/karma/

Custom build used in karma test:

git clone git://github.com/jquery/jquery.git
git checkout 2.1.0
npm install
grunt custom:-sizzle,-css,-effects,-offset,-dimensions,-deprecated,-ajax/script,-ajax/jsonp,-wrap,-event-alias,-exports/amd
cp ./dist/jquery.min.js /path/to/sinon-doublist/lib/jquery.js