LoginSignup
0
0

More than 5 years have passed since last update.

VCRとrequest specを併用したときに詰まったことのメモ

Last updated at Posted at 2017-11-28

なにこれ?

VCRでRailsの外部API呼び出しを行っているときに、詰まったことと解決方法のメモ

問題

Request specを使ったときにController(やModel)側でAPI callがあったときにエラーが起きる

RSpec.describe "Hoge", type: :request, vcr: true do
  describe 'GET /hoge' do
    subject do
      get '/hoge'
      response
    end
    it { is_expected.to have_http_status 200 }
  end
end

この時エンドポイント /hogeでエラーが起こる

解決方法

VCR.use_cassetteを使ってない場合、APIコール時にexample.descriptionを取りに行き、取れずに失敗している。

応急的には

RSpec.describe "Hoge", type: :request, vcr: true do
  describe 'GET /hoge' do
     subject do |example|
      VCR.use_cassette example.description do
        get '/hoge'
      end
      response
    end
    it { is_expected.to have_http_status 200 }
  end
end

で動いた。

場当たり的ではない対策はちょっと考える。

(VCRのリポジトリ上で既知で無いならPR、既知ならきっと良いやり方があるはず)

もっと良い方法募集中

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0