Toggleable Fakes for better integration testing
30 Sep 2014During the development of iOS Project Monitor, I ran into the need to test against a few third party services. Engineyard’s Andy Delcambre gave a great talk on how to use test matrices and fake services to write one test that hits both your fake and the service.
Most people leap towards WebMock or VCR to test against services. This is an alternate approach that is a little more upfront work but adds incredible confidence in your code.
Overview
- Create a
FakeThirdParty
service.
- Using WebMock, route all traffic to
www.thirdparty.com
to our sinatra fake. Take note of the.to_rack
method:
- Using an environment variable,
INTEGRATION=true
, you can turn off the WebMock routing and have the tests hit the real service.
Challenges
- Performing a Tear down, or cleaning, your third party test environment can be hard, if not impossible.
- Data from one test can then pollute other tests.
For something a little more elaborate, check out iOS Project Monitor’s Fake Parse Service and the WebMock Routing.
[16M05] Toggleable Mocks and Testing Strategies in a Service Oriented Architecture (en) from rubykaigi on Vimeo.