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

Testing Third Party APIs

Testing Third Party APIs

Dives into various strategies for Testing Third Party APIs.

Stephanie Viccari

December 21, 2020

Transcript

15 slides
  1. W y ho ld es s vo d PI al

    s? 4 D n t 4 F t 4 A h a r -l 4 P c g t a s p i e m 2
  2. S ub in t e da te RSpec.describe ShoppingCart do

    it "calculates tax" 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 # This approach is useful when unit-testing objects that # depend on data from other services. 5
  3. S ub in H TP eq es s RSpec.describe ShoppingCart

    do it "calculates tax" 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 7
  4. U in a ak A ap er RSpec.feature "signing in"

    do scenario "with two factors" do create(:user, password: "password", email: "[email protected]") visit root_path click_on "Sign In" fill_in :email, with: "[email protected]" fill_in :password, with: "password" click_on "Submit" last_message = FakeSMS.messages.last fill_in :code, with: last_message.body click_on "Submit" expect(page).to have_content("Sign out") end end 9
  5. # spec/support/fake_trello.rb class FakeTrello < Sinatra::Base get "/1/members/:id" do content_type

    :json JSON.generate(THOUGHTBOT_MEMBER.merge(id: params[:id])) end end # rails_helper.rb RSpec.configure do |config| config.before(:each) do stub_request(:any, /api.trello.com/).to_rack(FakeTrello) end end 1
  6. F at re eq es W I v h h

    e o h e r A r C , I n e h n , d , n v o h x u g e . 1
  7. O tl ne 4 S s G (i a n

    c ) 4 S T R s 4 S m A 4 R R a k A 1
  8. R so rc s C F o D e: -

    h ://t b .c /b /c -f -f -d e T I t t 3 -p I (v ): - h ://t b .c /u /v /t -i t - w -3 -p -a T T P I t : - h ://t b .c /b /t -t -p -i t 1

More Decks by Stephanie Viccari

Other Decks in Education