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

VCR

 VCR

Using VCR and webmock for stubbing API response

Revath S Kumar

September 21, 2014
Tweet

More Decks by Revath S Kumar

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. VCR : RECORD MODES VCR : RECORD MODES :once :new_episodes

    :none :all VCR 14 of 17 @revathskumar Sunday 21 September 2014 10:24 PM
  12. 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
  13. 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