Skip to main content

Join GoRails to continue learning

Subscribe to GoRails to get access to this episode and all other pro episodes, and access to our private Slack community of Ruby and Rails devs.

Subscribe to watch

Login to your account

Testing:

Testing API Integrations with WebMock

10

Episode 317 · October 23, 2019

Testing APIs


When your app integrates with external APIs, it can make testing your code quite tricky. Each API request needs unique IDs, returns results based upon the time, IDs, and other factors and can easily make your test suite break.

WebMock is a fantastic way to stub out and mock your HTTP requests. It will actually throw an exception in your test suite when an HTTP request is made so that you know when you're hitting an external API that you haven't stubbed out correctly.

To create a stub for an HTTP request with WebMock, you simply define the URL, method, params and headers that must match and then define the response's status code, body, and headers it should return. Simple as that.

This allows us to mock external API requests that function just like real ones. We don't have to change our code to get the test suite to run and we can be confident that everything is working.

Resources

Transcripts

No transcripts available. Earn a free month

Loading...

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.