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

VCR

 VCR

Using VCR and webmock for stubbing API response

Fb7edc7cc7a53c903af74d07dcecf9dc?s=128

Revath S Kumar

September 21, 2014
Tweet

Transcript

  1. VCR VCR @revathskumar VCR 1 of 17 @revathskumar Sunday 21

    September 2014 10:24 PM
  2. ABOUT ABOUT Rubyist / JavaScripter Team Member / / Works

    at Blog at Twitter/Github - Google+: Yeoman @keralarb @keralajs @whatznear blog.revathskumar.com @revathskumar +RevathSKumar VCR 2 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  3. WHATZNEAR ARCHITECTURE WHATZNEAR ARCHITECTURE VCR 3 of 17 @revathskumar Sunday

    21 September 2014 10:24 PM
  4. WEBMOCK WEBMOCK Stubbing HTTP requests at low http client lib

    level Support multiple HTTP library Setting and verifying expectations on HTTP requests Matching requests based on method, URI, headers and body VCR 4 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  5. SETUP SETUP # Gemfile group :test do gem 'webmock' end

    # test/test_helper.rb require 'webmock/test_unit' VCR 5 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  6. STUBBING STUBBING stub_request(:post, "www.example.com"). with(:body => {:data => {:a =>

    '1', :b => 'five'}}) stub_request(:get, "www.example.com"). to_return(body: "{:data => {:a => '1', :b => 'five'}}", status: 200) VCR 6 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  7. STUBBING FROM FILE STUBBING FROM FILE # test/test_helper.rb require 'webmock/test_unit'

    class ActiveSupport::TestCase def load_fixtures name path = File.join(Rails.root, "test", "fixtures", "#{name}.json") return nil unless File.exists?(path) File.read path end end stub_request(:any, "www.example.com"). to_return(:body => load_fixtures('get_products'), :status => 200) VCR 7 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  8. BUT GOT INTO TROUBLES BUT GOT INTO TROUBLES half of

    my time went to setup and maintain fixture thought of setting up factories, but still problem with structure of JSON response VCR 8 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  9. VCR VCR Make first request to API and save the

    response replay the saved reponse from second request onwards VCR 9 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  10. VCR 10 of 17 @revathskumar Sunday 21 September 2014 10:24

    PM
  11. SETUP SETUP # test/test_helper.rb require 'vcr' require 'webmock/minitest' VCR.configure do

    | c | c.cassette_library_dir = 'test/fixtures/vcr_cassettes' c.hook_into :webmock end VCR 11 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  12. VCR.use_cassette 'carts/empty', record: :new_episodes do get :show, format: :json assert_response

    :success assert_template 'show.json' assert_equal 0, JSON.parse(@response.body)['count'] end VCR 12 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  13. test 'remove item' do VCR.use_cassette 'carts/delete', record: :new_episodes do item

    = {"id"=>"2816", "uri"=>"",..} store = {"id"=>"prestige", "name"=>"Prestige Smart Kitchen"} post :create, format: :json, item: item, store: store_fixture delete :destroy, format: :json, id: 2816, store: 'prestige' assert_response :success assert_template 'show.json' assert_equal 0, JSON.parse(@response.body)['count'] end end VCR 13 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  14. VCR : RECORD MODES VCR : RECORD MODES :once :new_episodes

    :none :all VCR 14 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  15. REQUEST MATCHING REQUEST MATCHING VCR.use_cassette 'carts/empty', record: :new_episodes, match_requests_on: [:method,

    :uri, :body] do get :show, format: :json assert_response :success assert_template 'show.json' assert_equal 0, JSON.parse(@response.body)['count'] end :method :uri :body :host :path :headers :query VCR 15 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  16. class UsersControllerTest < ActionController::TestCase def setup VCR.insert_cassette 'users', record: :new_episodes

    end def teardown VCR.eject_cassette 'users' end ... end VCR 16 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  17. VCR 17 of 17 @revathskumar Sunday 21 September 2014 10:24

    PM