Book Image

Backbone.js Testing

By : Ryan Glenn Roemer
Book Image

Backbone.js Testing

By: Ryan Glenn Roemer

Overview of this book

Table of Contents (13 chapters)
Backbone.js Testing
About the Author
About the Reviewers

Spicing up Chai with the Sinon.JS plugin

One of the primary motivations for using the Chai library is the natural language syntax of its chained assertions. Another strong point of Chai is that it produces clear error messages on assertion failures.

Unfortunately, Sinon.JS spies create some assertion challenges within our test framework. To illustrate the issue, let's focus on the previous example that asserts that the obj.multiply() method (wrapped in a spy) was called with the parameters 5 and 2.

At this point, we have encountered two ways of making assertions on Sinon.JS spies—with Chai assertions and with Sinon.JS built-in spy assertions. Starting with the first method, we can write a Chai assertion on the spy as follows:

expect(obj.multiply.calledWith(5, 2));

However, a drawback of this statement is that if the assertion fails, Chai will produce the unhelpful error message expected false to be true.

We can get a much better error message, AssertError: expected multiply to be called...