Skip to content
Sinon.JS test double mixins
JavaScript
Failed to load latest commit information.
config/grunt
dist
docs
lib fix(lib): use `let` in `for` loops for browserify compat Apr 15, 2015
test
.gitignore
.travis.yml fix(travis): incorrect version Apr 15, 2015
CHANGELOG.md docs(browser): add usage notes Apr 15, 2015
Gruntfile.js
LICENSE
README.md
index.js
karma.conf.js
package.json

README.md

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.

Build Status

Examples

Mixin (recommended)

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

Gotchas

useFakeTimers and setImmediate

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

Installation

NPM

npm install sinon-doublist

In browsers

dist/sinon-doublist.js provides a sinonDoublist global that is the same object available in the node environment via require('sinon-doublist).

Related Projects

API

Documentation

License

MIT

Tests

Node

npm test

Browser via Karma

  • npm install karma karma-mocha
  • Optionally create karma-local.conf.js that exports an object with custom karma config key/value pairs.
  • ./node_modules/karma/bin/karma start
  • Browse http://localhost:9876/karma/

jQuery 2.1.0

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
Something went wrong with that request. Please try again.