Search⌘ K

The Service Integration Test

Explore testing external service integrations in Rails by writing integration tests using the Twitter API. Understand how to configure VCR and Webmock gems, manage API keys securely, and verify user data like Twitter avatars alongside your application tasks.

Twitter integration testing

We’ll use the Twitter gem to interface with Twitter. We’re also going to need the VCR and Webmock gems in the test environment. We add them to the Gemfile:

Ruby
gem 'twitter'
gem 'vcr', group: :test
gem 'webmock', group: :test

Then we’ll also have to reinstall the bundle with bundle install.

Twitter API and secret key

We need a Twitter API key and a secret key, which we can get from the Twitter Application Management page. In Rails 5 or higher, those get placed in the secrets.yml file, which typically is not stored in our code repository, which we’ve put in the config directory:

YAML
shared:
api_key: 123
development:
secret_key_base: <your_secret_key_base>
test:
secret_key_base: <your_secret_key_base>
twitter_api_key: "<your_api_key>"
twitter_api_secret: "<your_api_secret_key>"
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

Note: ...