Sinon.JS is a great library created by Christian Johansen, author of the great book, Test-Driven JavaScript Development, to make easy dealing with Stubs, Spies, and Mocks.
Although Jasmine already has support for Stubs and Spies, we are going to use a specific functionality of Sinon.JS to test AJAX requests, its FakeXMLHttpRequest
and FakeServer
functions.
The main difference between a Stub and a Fake, as you will see with the FakeXMLHttpRequest
object, is that a Fake is like a simpler but still complete implementation of a real component, and it is usually set at a system level.
Before we dig into the spec implementation, first we need to add Sinon.JS to the project. Go to http://sinonjs.org/ and download the current release, placing it inside the lib
folder.
We also need to add it to the SpecRunner.html
file, so go ahead and add another script:
<script type="text/javascript" src="lib/sinon.js"></script>