"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
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
: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
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