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

Caracal

ogom
February 21, 2015

 Caracal

ogom

February 21, 2015
Tweet

More Decks by ogom

Other Decks in Programming

Transcript

  1. Web API ։ൃͷݟ͑ΔԽ API ͷ Request ͱ Response ͕ݟ͑Δঢ়ଶ ʂʁ

    ͦΕͬͯ υΩϡϝϯςʔγϣϯ ͷ͜ͱ͡Όͳ͍ͷ ʂʂ 3/15
  2. RSpec Ͱ VCR Λར༻ͨ͠։ൃ Everyday Rails Testing with RSpec Ͱ͸

    ͦͷଞͷςετͷʮWeb αʔϏεΛςετ͢ΔʯͰղઆ 4/15
  3. VCR (Video Cassette Recording) Original Example https://relishapp.com/vcr/vcr/docs require 'test/unit' require

    'vcr' VCR.configure do |config| config.cassette_library_dir = "fixtures/vcr_cassettes" config.hook_into :webmock # or :fakeweb end class VCRTest < Test::Unit::TestCase def test_example_dot_com VCR.use_cassette("synopsis") do response = Net::HTTP.get_response(URI('http://www.iana.org/domains/reserved')) assert_match /Example domains/, response.body end end end 5/15
  4. VCR (Video Cassette Recording) RSpec Example require 'rspec' require 'vcr'

    VCR.configure do |config| config.cassette_library_dir = "fixtures/vcr_cassettes" config.hook_into :webmock # or :fakeweb end RSpec.describe "VCRTest" do it "test_example_dot_com" do VCR.use_cassette("synopsis") do response = Net::HTTP.get_response(URI('http://www.iana.org/domains/reserved')) expect(response.body).to match(/Example domains/) end end end 6/15
  5. ه࿥͢ΔϝΧχζϜ after_request ϝιουͰ Request ͱ Response ΛϑΝΠϧʹॻࠐΈ require 'webmock' WebMock.after_request

    do |request, response| puts "Request: #{request.uri}" puts "Response: #{response.body}" end url = 'www.example.com' WebMock.stub_request(:get, url).to_return(body: 'OK') Net::HTTP.get(url, '/') 7/15
  6. ࠶ੜ͢ΔϝΧχζϜ use_cassette ϝιουͰϑΝΠϧΛಡࠐΈ Request ͱ Response ʹม׵ VCR.use_cassette("root") do |cassette|

    interaction = cassette.http_interactions.interactions.first expect(interaction['response']['status']['code']).to eq(200) end 8/15
  7. VCR ͷݟ͑ΔԽ Mr. Video ( Rails engine for managing VCR

    cassettes ) Rails Ͱ͔͠࢖͑ͳ͍ʁ ΫϥΠΞϯτͷ։ൃʹ࢖͑ͳ͍ʂ 9/15
  8. VCR ͷݟ͑ΔԽ CassetteRack ( Rack for managing VCR cassettes )

    Λ࡞ͬͨ ʂ Rack (rackup) ͰΫϥΠΞϯτͷݟ͑ΔԽ ։ൃʂ 10/15
  9. Rails ͷݟ͑ΔԽ Rails ͷςετͰ HTTPॲཧ ͸ ActionDispatch::TestRequest ͕࣮ࢪ ͳͷͰ ActionDispatch::TestRequest

    Λ CassetteRack::Request ʹࠩସ͑ Rails (rackup) Ͱαʔόͷݟ͑ΔԽ ։ൃʂ 11/15
  10. υΩϡϝϯςʔγϣϯ ͷ࡞੒ VCR ͷ Request ͱ Response ΛϑΝΠϧʹग़ྗ ςϯϓϨʔτΤϯδϯʹ Liquid

    Λ࠾༻ ### Request ~~~ {{ request.method }} {{ request.path }} ~~~ ### Response ~~~ {{ response.status_code }} {{ response.status_message }} {{ response.body }} ~~~ 12/15