Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
WEB APP TESTING USING KNOCKOUT.JS

You're reading from  WEB APP TESTING USING KNOCKOUT.JS

Product type Book
Published in Nov 2014
Publisher
ISBN-13 9781783982844
Pages 154 pages
Edition 1st Edition
Languages
Author (1):
ROBERTO MESSORA ROBERTO MESSORA
Author Profile Icon ROBERTO MESSORA
ROBERTO MESSORA
Toc

Testing asynchronous code


Jasmine can test asynchronous code using an alternative specification pattern. The beforeEach, afterEach, and it global functions can receive an argument, which is a particular helper function that we have to use in this situation.

The asynchronous test pattern can be summarized in the following example (03_07.js). Think about a typical service that executes an ajax request:

var MyService = function () {
    this.fetchResult = function (callback) {
        jQuery.ajax("url", {
            success: function (result) {
                callback(result);
            }
        });
    };
};

The fetchResult method takes a callback function as an argument, which is called by passing the result when the Ajax call is successful.

If we want to test this JavaScript class, we can write the following spec (03_07_specs.js):

describe("Given an async service", function () {
    var myService, myResult;
    beforeEach(function (done) {
        myService = new MyService();
        spyOn...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime