Testing Ajax Calls

Now that you have the internal logic of your task list working, let’s focus on how to get data into and out of this code. The previous version of the code read the tasks essentially from the DOM, but in this case you’ll make a separate Ajax call to get that information from the server. Let’s talk about strategies to test Ajax calls.

Here’s the basic structure of an Ajax call—this one uses jQuery, but the basic idea holds no matter how the call gets made:

 $.ajax({
  url: ​`projects/​${​this​.id}​.js`
  dataType: ​"json"
 }).then((data) => {
 this​.name = data.project.name
 // and so on
 })

The jQuery $.ajax method takes the URL and other information about the call you want to make and returns a JavaScript promise ...

Get Rails 5 Test Prescriptions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.