Book Image

Jasmine JavaScript Testing Update

By : Paulo Vitor Zacharias Ragonha
Book Image

Jasmine JavaScript Testing Update

By: Paulo Vitor Zacharias Ragonha

Overview of this book

Table of Contents (15 chapters)

Writing the spec


With the server running, open your browser at http://localhost:8000/SpecRunner.html to see the results of our specs.

You can see that even though the server is running, and the spec appears to be correct, it is failing. This is due to the fact that stock.fetch() is asynchronous. A call to stock.fetch() returns immediately, allowing Jasmine to run the expectations before the AJAX request is completed:

it("should update its share price", function() {
  expect(stock.sharePrice).toEqual(20.18);
});

To fix this, we need to embrace the asynchronicity of the stock.fetch() function and instruct Jasmine to wait for its execution before running the expectations.

Asynchronous setups and teardowns

In the example shown, we invoke the fetch function during the spec's setup (the beforeEach function).

The only thing we need to do to identify that this setup step is asynchronous is add a done argument to its function definition:

describe("when fetched", function() {
  beforeEach(function(done)...