Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rich Javascript Assertions Part 2: Sinon

Rich Javascript Assertions Part 2: Sinon

A quick description of the assertions provided by Sinon

Danielle Brook-Roberge

August 02, 2016
Tweet

More Decks by Danielle Brook-Roberge

Other Decks in Programming

Transcript

  1. Rich Assertions • Javascript testing libraries provide a wide variety

    of test assertions. • Beyond the “basic” assertions of truth, falseness, equality, and type, we have “rich” assertions which assert something more specific. • Rich assertions allow both clearer code and more explanatory error messages on failure. • This talk describes rich assertions in Sinon.
  2. Sinon • Sinon is a function mocking library for Javascript.

    • It can stub out or spy upon functions, and then allows access to the call count, arguments, return values, etc. • The Sinon rich assertions can be included in the Chai assert object by doing: sinon.assert.expose(chai.assert, { prefix: "" });
  3. assert.calledOnce • assert.isTrue(stub.calledOnce); becomes assert.calledOnce(stub); • On failure, this prints

    the name of the stub and the actual call count. • There are also calledTwice and calledThrice assertions.
  4. assert.notCalled • assert.isFalse(spy.called); becomes assert.notCalled(spy); • On failure, it shows

    the name of the stub and the actual call count, just as with calledOnce.
  5. assert.calledWith • assert.isTrue(stub.calledWith(arg1,...)); assert.equal(stub.args[0][0], arg1); become assert.calledWith(stub, arg1...); • On

    failure, it shows the stub name and intended arguments • This assertion allows incomplete matching of argument lists.
  6. assert.callOrder • assert.callOrder(stub1, stub2,...); passes if and only if the

    stubs are called in the specified order. • I don’t actually know how to do this without the rich assertion.