Consuming external APIs from Ruby [1d]
On the Movie DB index, show a random actor who was born today:
- There's an XML feed for this at
- Parse the feed using Nokogiri
- Update it once a day (using )
Automatically retrieve the year of a movie for any new movie entered into MovieDB, using the. For the UI this means that there is no longer a "Year" field when creating a movie. It is automatically fetched and stored before the movie is created. When editing a movie there is a "Year" field that can be changed.
Write RSpec tests for both exercises. Write multiple variants of this test, each using a different approach:
- Just call the real API from the example
- Use plain RSpec mocks ("stubs") to replace the HTTP request to the API with scripted behavior. Can you minimize the number of lines of code that now no longer run during tests?
- Mock out the network request to the API using Webmock
- Mock out the network request to the API using VCR. What happens when you change the movie title in the test?
Talk to your mentor about the pros and cons of each approach.
Together with your mentor, find out how to optimize your test suite using a combinations of the techniques you learned. Also consider whether form models can help here.
Your final test suite should have the following properties:
- It should not talk to the network while it runs (it's OK to talk to the network once in order to record a VCR cassette).
- It should not record an excessive number of VCR cassettes. In particular, not every test that saves a movie should record a cassette.
- It should run (approximately) as fast as it did before the changes from this card.
- Tests that don't care about the features from the XML (birthday) and JSON (movie year) exercises should not require additional code to mock the network or otherwise silence impractical side effects from the changes in the card.
Implement this in your MovieDB.