VCR::Errors::UnhandledHTTPRequestError в тесте контроллера с использованием VCR

Я пытаюсь смоделировать вызов API, в котором я загружаю файл, я использую Wistia Upload API и, поскольку я не хочу подключаться к серверу при каждом тесте, я пробую видеомагнитофон впервые.

У меня есть следующий тест в моей папке spec / controllers:

let(:file) { Rack::Test::UploadedFile.new("video_path", 'video/mp4') }

describe "GET #index" do
  it "assigns all videos as @videos" do
    VCR.use_cassette "wistia/upload" do 
      video = Video.create! valid_attributes

      get :index, {}
      expect(assigns(:videos)).to eq([video])
    end
  end
end

конец

Вызов API запускается внутри обратного вызова модели, который выглядит следующим образом:

class Video < ActiveRecord::Base
  after_save :move_video
  def move_video
    uri = URI('https://upload.wistia.com/')

    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true

    # Construct the request.
    request = Net::HTTP::Post::Multipart.new uri.request_uri, {
    'api_password' => '',
    'project_id'   => ''
    'file' => my_video_file
    }
    # Make it so!
    response = http.request(request)
    return response
  end
end

Я отладил вызов этого метода, и он вызывается правильно, но когда я запускаю свой тест, я получаю следующую ошибку:

1) VideosController GET #index assigns all videos as @videos
     Failure/Error: video = Video.create! valid_attributes
     VCR::Errors::UnhandledHTTPRequestError:


       ================================================================================
       An HTTP request has been made that VCR does not know how to handle:
         POST https://upload.wistia.com/

       VCR is currently using the following cassette:
         - /Users/urielhernandez/Documents/pf/spec/vcr/wistia/upload.yml
         - :record => :once
         - :match_requests_on => [:method, :uri]

       Under the current configuration VCR can not find a suitable HTTP interaction
       to replay and is prevented from recording new requests. There are a few ways
       you can deal with this:

         * If you're surprised VCR is raising this error
           and want insight about how VCR attempted to handle the request,
           you can use the debug_logger configuration option to log more details [1].
         * You can use the :new_episodes record mode to allow VCR to
           record this new request to the existing cassette [2].
         * If you want VCR to ignore this request (and others like it), you can
           set an `ignore_request` callback [3].
         * The current record mode (:once) does not allow new requests to be recorded
           to a previously recorded cassette. You can delete the cassette file and re-run
           your tests to allow the cassette to be recorded with this request [4].
         * The cassette contains an HTTP interaction that matches this request,
           but it has already been played back. If you wish to allow a single HTTP
           interaction to be played back multiple times, set the `:allow_playback_repeats`
           cassette option [5].

       [1] https://www.relishapp.com/vcr/vcr/v/2-9-3/docs/configuration/debug-logging
       [2] https://www.relishapp.com/vcr/vcr/v/2-9-3/docs/record-modes/new-episodes
       [3] https://www.relishapp.com/vcr/vcr/v/2-9-3/docs/configuration/ignore-request
       [4] https://www.relishapp.com/vcr/vcr/v/2-9-3/docs/record-modes/once
       [5] https://www.relishapp.com/vcr/vcr/v/2-9-3/docs/request-matching/playback-repeats
       ================================================================================

Кассета была создана, но я получил указанную ошибку. После того, как я удалил кассету, чтобы увидеть, генерировалась ли она, она не была сгенерирована повторно, и видеомагнитофон не обрабатывает запрос.

1 ответ

Решение

Кассета создается в /Users/urielhernandez/Documents/pf/spec/vcr/wistia/upload.yml как описано в сообщении об ошибке.

Пытаться VCR.use_cassette("wistia/upload", :record => :new_episodes) do; это запишет это как новый запрос в той же кассете.

Подробнее о режимах записи vcr читайте по адресу https://www.relishapp.com/vcr/vcr/v/1-3-2/docs/record-modes

Другие вопросы по тегам