In the previous post we have seen how to get started with Jasmine by testing a simple function that validates an email address.
But most of the time we want to test JS that runs inside the browser and interacts with the DOM.
We also want to test Ajax calls and mocking the output from the server, which of course is not running during the tests.
You will usually interact with the DOM using some library like JQuery.
Using Jasmine-JQuery you can integrate it easily with Jasmine.
For mocking server response instead Jasmine Spies comes in very handy.
Suppose we keep the same folder structure as in the previous example.
Of course we need to download JQuery and JQuery-Jasmine, place them respectively under src and lib, finally add the following 2 lines to the head section in SpecRunner.html after the jasmine imports:
So
let's test a JQuery plugin which downloads from the server an html
snippet containing some list of friends and set it as Html of each
element in the JQuery's wrapped set of elements.
This code goes inside src/demo.js, while the test has to be placed inside spec/demo_spec.js
So let's see step by step what we is happening here:
Spies are in my opinion the greatest feature in Jasmine, the following functions facilitate mocking:
It is indeed possible to verify function calls using the following matchers:
One last feature I have not covered in these posts are Asynchronous Specs, which come handy if you want to simulate different asynchronous events occurring in different order.
But most of the time we want to test JS that runs inside the browser and interacts with the DOM.
We also want to test Ajax calls and mocking the output from the server, which of course is not running during the tests.
You will usually interact with the DOM using some library like JQuery.
Using Jasmine-JQuery you can integrate it easily with Jasmine.
For mocking server response instead Jasmine Spies comes in very handy.
Suppose we keep the same folder structure as in the previous example.
Of course we need to download JQuery and JQuery-Jasmine, place them respectively under src and lib, finally add the following 2 lines to the head section in SpecRunner.html after the jasmine imports:
- <script type="text/javascript" src="lib/jasmine-jquery.js"></script>
- <script type="text/javascript" src="src/jquery-1.5.1.min.js"></script>
- $(function(){
- jQuery.fn.addFriendList = function(){
- var that = this;
- $.ajax({
- url: 'friends',
- type: 'GET',
- contentType: 'application/json',
- dataType: 'json',
- success: function(html){
- $(that).html(html);
- }
- });
- }
- });
- describe("Download friends", function() {
- it("should get the list of friends and insert it in the page",function(){
- setFixtures('<div id="friends"></container>');
- spyOn(jQuery,'ajax');
- $('#friends').addFriendList();
- jQuery.ajax.mostRecentCall.args[0].success('<ul><li>Arturo</li><li>Concetto</li></ul>');
- expect($('#friends li').length).toBe(2);
- });
- });
So let's see step by step what we is happening here:
- first we define our test suite by calling
describe() - a call to
it()define our test which "should get the list of friends and insert it in the page" setFixtures()comes from Jasmine-JQuery and allows you to insert html into the SpecRunner's DOM. WhilesetFeatures()takes as input a string,loadFixtures()load the html from an external file whose path has passed toloadFixtures(). The default path is spec/javascript/fixtures. But since our test is very simple I preferred using the inline version and to just add a div to the pagespyOn(jQuery,'ajax')allows us to "spy" on any call toajax()$('#friends').addFriendList()just calls our pluginmostRecentCallis one the fields injasmine.Spy, it gives access to the last call to the spied method. In this case we are calling the success callback with a mocked html.- finally we check the div has been modified correctly. Actually a lot of expectations come with Jasmine-JQuery but in this case I preferred not to use any
Spies are in my opinion the greatest feature in Jasmine, the following functions facilitate mocking:
andCallFake(fakeFunction),andReturn(),andThrow()basically allows you to determine the output from a call to the spied functionandCallThrough()calls the original implementation and it is useful if you want to just verify functions calls
It is indeed possible to verify function calls using the following matchers:
toHaveBeenCalled()andtoHaveBeenCalledWith()verifies a function has been called (and with what parameters)wasNotCalled()andwasNotCalledWith()verifies a function has not been called (and with what parameters)
One last feature I have not covered in these posts are Asynchronous Specs, which come handy if you want to simulate different asynchronous events occurring in different order.

