Skip to content

Latest commit

 

History

History
91 lines (82 loc) · 2.97 KB

spy.md

File metadata and controls

91 lines (82 loc) · 2.97 KB

Sinon Spy API Mapping

The sinon Spy API provides many methods that return true if the spy was used in a particular way. The API's are used to implement these assertions:

And these properties:

When the Spy API returns a Spy Call, see spyCall.

If you are familiar with the sinon API, however, the following table shows the mapping of those API's to equivalent assertions.


spy.callCount === 7 expect(spy).to.be.called(7)
spy.called expect(spy).to.be.called()
spy.calledOnce expect(spy).to.be.called(1)
spy.calledTwice expect(spy).to.be.called(2)
spy.calledThrice expect(spy).to.be.called(3)
spy.firstCall expect(spy).firstCall.to.be...
spy.secondCall expect(spy).secondCall.to.be...
spy.thirdCall expect(spy).thirdCall.to.be...
spy.lastCall expect(spy).lastCall.to.be...
spy.calledOn(obj) expect(spy).to.be.calledOn(obj)
spy.calledWith(a, b, ...) expect(spy).to.be.calledWith(a, b, ...)
spy.calledWithExactly(a, b, ...) expect(spy).to.be.exactly.calledWith(a, b, ...)
spy.calledWithMatch(a, b, ...) expect(spy).to.match.calledWith(a, b, ...)
spy.alwaysCalledOn(obj) expect(spy).to.be.always.calledOn(obj)
spy.alwaysCalledWith(a, b, ...) expect(spy).to.be.always.calledWith(a, b, ...)
spy.alwaysCalledWithExactly(a, b, ...) expect(spy).to.be.always.exactly.calledWith(a, b, ...)
spy.alwaysCalledWithMatch(a, b, ...) expect(spy).to.always.match.calledWith(a, b, ...)
spy.neverCalledWith(a, b, ...) expect(spy).to.not.be.calledWith(a, b, ...)
spy.neverCalledWithMatch(a, b, ...) expect(spy).to.not.match.calledWith(a, b, ...)

With sinon, there is no API that combines always, match and exactly, however, this can be done with this add-on:

expect(spy).to.always.exactly.match.calledWith(a, b, ...);