Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testing Third Party Integrations

Testing Third Party Integrations

Dive into several strategies for testing third party interactions, with an emphasis on:

- stubbing at the network layer
- stubbing the adapter
- using a fake adapter

B77afd7977c10e820f4821acd38a1f09?s=128

Stephanie Viccari

January 06, 2021
Tweet

Transcript

  1. Testing Third Party Interactions 1

  2. Why Should Tests Avoid API Calls? • Deterministic tests •

    Faster tests • Prevent changing data on a staging or production environment 2
  3. Strategies for Mocking API Calls 3

  4. Stub Methods on Adapter 4

  5. Stubbing the Adapter RSpec.describe ShoppingCart do describe "#tax" do it

    "calculates a 10% tax rate" do item = create(:item) shopping_cart = ShoppingCart.new(item) allow(QuoteService).to receive(:fetch).and_return(100) expect(shopping_cart.tax).to eq 10 end end end # This approach is useful when unit-testing objects that # depend on data from other services. 5
  6. Stub HTTP Requests 6

  7. Stubbing HTTP Requests RSpec.describe ShoppingCart do describe "#tax" do it

    "calculates a 10% tax rate" do item = create(:item) shopping_cart = ShoppingCart.new(item) stub_request("http://quote-service.com/prices").and_return( { pricesRequest: { regularPrice: 100 } }.to_json ) expect(shopping_cart.tax).to eq 10 end end end 7
  8. Use a Fake Adapter 8

  9. Using a Fake Adapter RSpec.describe ShoppingCart do describe "#tax" do

    it "calculates a 10% tax rate" do item = create(:item) fake_quote_service = FakeQuoteService.new(price: 100) shopping_cart = ShoppingCart.new(item, quote_service: fake_quote_service) expect(shopping_cart.tax).to eq 10 end end end 9
  10. Real Requests to a Fake Service 10

  11. # spec/support/fake_quote_service.rb class FakeQuoteService < Sinatra::Base get "/prices/:id" do content_type

    :json status 200 { pricesRequest: { regularPrice: 100 } }.to_json end end # rails_helper.rb RSpec.configure do |config| config.before(:each) do stub_request(:any, /quote-service.com/).to_rack(FakeQuoteService) end end 11
  12. Feature Request When I visit the homepage for the New

    York Adventure Club, I want to see the name, date, and venue for the next upcoming event. 12
  13. Outline • Shameless Green (issue real network calls) • Stub

    HTTP Requests • Stub methods on an Adapter • Replace the Adapter with a Fake Adapter 13
  14. Helpful Links New York Meetup Event - https://www.meetup.com/NewYorkAdventureClub Meetup API

    - https://www.meetup.com/meetup_api 14
  15. Resources Custom Formats for DateTime - https://thoughtbot.com/blog/custom-formats-for-datetime Testing Third Party

    Interactions - https://thoughtbot.com/blog/testing-third-party- interactions Testing Interaction with 3rd-party APIs - https://thoughtbot.com/upcase/videos/testing- interaction-with-3rd-party-apis How to Stub External Services in Tests - https://thoughtbot.com/blog/how-to-stub-external- services-in-tests Faking APIs in Development and Staging - https://thoughtbot.com/blog/faking-apis-in-development- and-staging 15