Posted almost 5 years ago. Visible to the public.

Consuming external APIs with JavaScript [2d]

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.


  • When using Unpoly, you can not load <script> tags as part of a page fragment. You may put the <script> tag that loads Google Maps in your application layout (not ideal, discuss with your mentor why) or load it on demand as described in a separate card.
  • 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 for this.


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 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:

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