sinon-doublist

Sinon.JS test double mixins

npm install sinon-doublist
14 downloads in the last week
23 downloads in the last month

sinon-doublist

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

  • Double multiple methods in one call
  • sinon.testCase-like auto-sandboxing
  • Optional use of plain empty objects to hold method doubles
  • Method selection via x.y.z property path strings

Build Status

Examples

sinonDoublist(sinon, 'mocha');

describe('myFunction', function() {
  it('should do something', function() {
    // this.spyMany()
    // this.stubMany()
    // this.stubWithReturn()
  });
});

Mixin (manual)

describe('myFunction', function() {
  beforeEach(function() {
    sinonDoublist(sinon, this);
  });

  afterEach(function() {
    this.sandbox.restore();
  });

  it('should do something', function() {
    // this.spyMany()
    // this.stubMany()
    // this.stubWithReturn()
  });
});

spyMany()

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();

stubMany()

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();

stubWithReturn()

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);

stubBind()

function target() {}
function fakeBoundTarget() {}

var stub = this.stubBind(target, null, 1, 2, 3).bind;
stub.bind.returns(fakeBoundTarget);

target.bind(null, 3, 2, 1); // undefined
console.log(stub.bind.called); // false

target.bind(null, 1, 2, 3); // fakeBoundTarget
console.log(stub.bind.called); // true

Installation

component

component install codeactual/sinon-doublist

NPM

npm install sinon-doublist

API

Documentation

License

MIT

Tests

Node

npm test

Browser via Karma

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

jQuery 2.0

Custom build used in karma test:

grunt custom:-sizzle,-css,-effects,-offset,-dimensions,-deprecated,-ajax/script,-ajax/jsonp,-wrap,-event-alias
npm loves you