Posted over 3 years ago. Visible to the public.

Consuming external APIs with Javascript

Example 1: Google Maps

  • In MovieDB, add a new field “Principal filming location”.
  • In a movie’s show view, geocode that location and show a Google map centered around it
  • Now write a Cucumber feature that tests that the map shows the correct location.

Hint:

  • Your JavaScript that creates and centers the map should expose an API that lets you query the current map position from your test.
  • Your test can use evaluate_script(...) to talk with that JavaScript.

Example 2: Gender API

  • In MovieDB, add a field "gender" to the Actor model.
    • For the purpose of this exercise, the field is an enum with the values male, female and other.
    • Use assignable_values to implement the enum.
  • In the actor form automatically suggest a gender as the user types a name
    • Also read this card for an issue you might encounter.
    • The user should be able to override the suggestion.
  • Use https://gender-api.com/ for this.

Testing

Write a Cucumber feature that tests the functionality above. Use two different techniques to write the test:

  1. Actually talk to the Gender API during tests.
  2. Only in tests, replace the JavaScript function that talks to the Gender API, with a fake implementation that maps a fixed list of names to their genders.
    • It should have the same (callback-based) API as your original Gender API client.
    • The fixed list of names can be part of your fake implementation. If it helps your test, your fake implementation might also expose an additional API to change the list of names.

Talk with your mentor about the pros and cons of each approach.

Owner of this card:

Avatar
Henning Koch
Last edit:
about 1 month ago
by Jakob Scholz
Posted by Henning Koch to makandra Curriculum
This website uses cookies to improve usability and analyze traffic.
Accept or learn more