Wrapping a Third-Party Dependency

While high-fidelity fakes can save a lot of trouble, they can still leave your logic coupled to a third-party API. This coupling has a few downsides:

  • Your code will be exposed to the complexities of a (potentially) large API.
  • Changes to the dependency’s interface will ripple throughout your system.
  • Testing is difficult, since you can’t easily and safely replace the third-party code with a double.

To avoid these pitfalls, you can wrap the dependency—that is, write your own layer that delegates to it internally. Using a wrapper (also known as a gateway or adapter when you’re wrapping an API) gives you a couple of key advantages:

  • You can create a small, simple interface tailored exactly to your needs.
  • You can safely ...

Get Effective Testing with RSpec 3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.