Sitemap
Tyson Gern

Tyson Gern

Lead developer and cofounder at Initial Capacity

Testing External Services

1 min readAug 26, 2015

When working on web projects it is often necessary to integrate with an external service, such as Stripe or Twitter. It is important to be able to integration test these services so that we can be confident that our integrations are working. While many web services provide testing environments, it is easy to clutter our integrations when we are running test, development, acceptance, staging, and production environments.

Get Tyson Gern’s stories in your inbox

Join Medium for free to get updates from this writer.

I’ve developed a pattern that I like to use in a Capybara test suite to handle testing external services and facilitating the cleanup. Suppose we are integrating with a (ficticious) service called Zippy, which manages our users’ accounts, using a Zippy gem. I’d start by putting the following class in spec/support

Then I can use the ZippyEnvironment in my Capybara tests to ensure cleanup.

As long as I put my test code within the ZippyEnvironment block, I’m assured that I have access to my Zippy user and that the user will be removed when the test is completed. Note that since we are using ensure we are assured that users will be deleted even if the test fails or if an exception is raised.

--

--

Tyson Gern
Tyson Gern

Written by Tyson Gern

Lead developer and cofounder at Initial Capacity