Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Sinon.JS test double mixins
JavaScript
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
config/grunt/initConfig
dist
docs
lib
test
.gitignore
.travis.yml
CHANGELOG.md
Gruntfile.js
LICENSE
README.md
component.json
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

component

component install codeactual/sinon-doublist

NPM

npm install sinon-doublist

Related Projects

API

Documentation

License

MIT

Tests

Node

npm test

Browser via Karma

  • npm install karma
  • grunt build && 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.