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

RSpecによるOpen API自動テスト

fuqda
August 26, 2020

RSpecによるOpen API自動テスト

【オンライン開催】hey × Da Vinci Studio
〜プロダクトの成長を支えている裏側(技術と組織)〜の発表資料です。
https://hey.connpass.com/event/181259

fuqda

August 26, 2020
Tweet

More Decks by fuqda

Other Decks in Programming

Transcript

  1. About me fuqda (;ͩ͘) • 2017೥11݄ʙݱࡏ STORESόοΫΤϯυΤϯδχΞ (Ruby) • 2018೥2݄ʙݱࡏ

    Tama.rbͱ͍͏஍ҬRubyίϛϡχςΟ΍ͬͯ·͢ • झຯ͸ΪλʔͰ͢(ίϩφ͕མͪண͍ͨΒόϯυ׆ಈ͍ͨ͠) Twitter : ˏfuqda90
  2. Open APIͬͯͳʹʁ • REST APIͷAPIυΩϡϝϯτͷ هड़ϑΥʔϚοτͷ͜ͱ • ͦͷϑΥʔϚοτʹج͍ͮͯॻ͔Εͨ APIυΩϡϝϯτΛ ※Open

    API SpecificationͱݺͿ ※ ҰൠతʹOpen APIͱݴͬͨ৔߹͸ Open API SpecificationΛҙຯ͢Δ͜ͱ͕ଟ͍
  3. ओͳSwaggerπʔϧ • Swagger Edittor (ϒϥ΢β΍ϩʔΧϧͰOpen APIΛهड़ग़དྷΔΤσΟλʔ) • Swagger UI (Open

    APIͷهड़ΛಈతʹAPIυΩϡϝϯτͱͯ͠ϨϯμϦ ϯά͢Δπʔϧ) • Swagger Codegen (Open API͔Βαʔόʔ/ΫϥΠΞϯτͷίʔυΛੜ੒͢Δ πʔϧ)
  4. committee-railsͷ࢖͍ํ 
 
 describe 'Api::V1::BlogsController', type: :request do include Committee::Rails::Test::Methods

    def committee_options @committee_options ||= { schema_path: Rails.root.join('APIεΩʔϚͷύε'), old_assert_behavior: false } end describe 'GET /api/v1/blogs' do it 'Ϩεϙϯε͕APIυΩϡϝϯτͱҰக͢Δ' do get '/api/v1/blogs' assert_response_schema_confirm end end end
  5. committee-railsͷ࢖͍ํ 
 
 describe 'Api::V1::BlogsController', type: :request do include Committee::Rails::Test::Methods

    def committee_options @committee_options ||= { schema_path: Rails.root.join('APIεΩʔϚͷύε'), old_assert_behavior: false } end describe 'GET /api/v1/blogs' do it 'Ϩεϙϯε͕APIυΩϡϝϯτͱҰக͢Δ' do get '/api/v1/blogs' assert_response_schema_confirm end end end APIεΩʔϚͷಡΈࠐΈ
  6. committee-railsͷ࢖͍ํ 
 
 describe 'Api::V1::BlogsController', type: :request do include Committee::Rails::Test::Methods

    def committee_options @committee_options ||= { schema_path: Rails.root.join('APIεΩʔϚͷύε'), old_assert_behavior: false } end describe 'GET /api/v1/blogs' do it 'Ϩεϙϯε͕APIυΩϡϝϯτͱҰக͢Δ' do get '/api/v1/blogs' assert_response_schema_confirm end end end εΩʔϚݕূ༻ϝιουͷఆٛݩΫϥεͷಡΈࠐΈ
  7. committee-railsͷ࢖͍ํ 
 
 describe 'Api::V1::BlogsController', type: :request do include Committee::Rails::Test::Methods

    def committee_options @committee_options ||= { schema_path: Rails.root.join('APIεΩʔϚͷύε'), old_assert_behavior: false } end describe 'GET /api/v1/blogs' do it 'Ϩεϙϯε͕APIυΩϡϝϯτͱҰக͢Δ' do get '/api/v1/blogs' assert_response_schema_confirm end end end APIεΩʔϚͱ࣮ࡍͷAPIͷ஋ͷݕূ
  8. assert_request_schema (assert_response_schema)Ͱݕূ͞ΕΔύϥϝʔλ͸ required Ͱࢦఆͨ͠ϑΟʔϧυ͚ͩ post: summary: POST /api/v1/blogs description: ϒϩά࡞੒API

    tags: - blog requestBody: content: application/json: type: object properties: blog: type: object required: - title - content ݟͯΔͷίί͚ͩ
  9. ྫ͑͹͜Μͳ৔߹ͷ࿩ class BlogsController < ApplicationController ※தུ
 def create
 ※தུ begin

    ※ਖ਼ৗܥͷॲཧ rescue ::Mongoid::Errors::Validations render status: :bad_request, json: { errors: blog.errors } end end
  10. ྫ͑͹͜Μͳ৔߹ͷ࿩ class BlogsController < ApplicationController ※தུ
 def create
 ※தུ begin

    ※ਖ਼ৗܥͷॲཧ rescue ::Mongoid::Errors::Validations render status: :bad_request, json: { errors: blog.errors } end end ͦ΋ͦ΋ίίͷ෦෼ͷৄࡉΛͲ͜·Ͱ APIυΩϡϝϯτʹ࢒͔͢໰୊
  11. YAMLʹ͸࠷௿ݶͷεςʔλείʔυͱ Τϥʔ಺༰ͷ഑ྻ͕ฦͬͯ͘Δ͜ͱ͚ͩΛఆٛ responses: ※தུ '400': description: Bad Request content: application/json:

    schema: type: object properties: errors: type: array blogs: {} required: - errors Τϥʔ഑ྻ͕ඞਢͰ͋Δ ͜ͱ͚ͩఆٛ͢Ε͹Α͠ ͱͨ͠
  12. Content-Type ͕ text/csv ʹͳ͍ͬͯΔ͜ͱɺ εςʔλείʔυͷ֬ೝ ͕࠷௿ݶग़དྷΕ͹ OKͱ͢Δ͜ͱʹ requestBody: content: text/csv:

    schema: type: string responses: '200': description: OK content: text/csv: schema: type: string