Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Chapter 6<br />
"port":"8000",<br />
"client-encoding":"UTF-8"<br />
});<br />
});<br />
it("the spy was called", function() {<br />
expect(configurator.submitPOSTRequest).toHaveBeenCalled();<br />
});<br />
it("the arguments of the spy's call are tracked", function() {<br />
expect(configurator.submitPOSTRequest).toHaveBeenCalledWith({"port<br />
":"8000","client-encoding":"UTF-8"});<br />
});<br />
});<br />
In this example, while we are writing this test case, we either don't have the real<br />
implementation of the configurator.submitPOSTRequest() dependency or<br />
someone is fixing this particular dependency. In any case, we don't have it available.<br />
For our test to work, we need to mock it. Jasmine spies allow us to replace a function<br />
with its mock and track its execution.<br />
In this case, we need to ensure that we called the dependency. When the actual<br />
dependency is ready, we will revisit this test case to make sure that it fits the<br />
specifications, but at this time, all that we need to ensure is that the dependency is<br />
called. The Jasmine tohaveBeenCalled() function lets us track the execution of a<br />
function, which may be a mock. We can use toHaveBeenCalledWith() that allows<br />
us to determine if the stub function was called with the correct parameters. There<br />
are several other interesting scenarios that you can create using Jasmine spies. The<br />
scope of this chapter won't permit us to cover them all, but I would encourage you to<br />
discover these areas on your own.<br />
You can refer to the user manual for Jasmine for more information<br />
on Jasmine spies at http://jasmine.github.io/2.0/<br />
introduction.html.<br />
Mocha, Chai, and Sinon<br />
Though Jasmine is the most prominent <strong>JavaScript</strong> testing framework,<br />
Mocha and Chai are gaining prominence in the Node.js environment.<br />
Mocha is the testing framework used to describe and run test cases.<br />
Chai is the assertion library supported by Mocha. Sinon.JS comes in<br />
handy while creating mocks and stubs for your tests. We won't discuss<br />
these frameworks in this book, but experience on Jasmine will be handy<br />
if you want to experiment with these frameworks.<br />
[ 153 ]<br />
www.it-ebooks.info