ruby-on-rails - VCR::Errors::UnhandledHTTPRequestError 在使用 VCR 的 Controller 测试中

标签 ruby-on-rails vcr

我正在尝试模拟上传文件的 api 调用,我正在使用 Wistia Upload API因为我不想每次测试都打到服务器,所以我第一次尝试 VCR。

我的 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
       ================================================================================

磁带已创建,但我收到了提到的错误。在我删除磁带以查看它是否正在生成后,它没有重新生成,并且 VCR 没有处理请求。

最佳答案

正在 /Users/urielhernandez/Documents/pf/spec/vcr/wistia/upload.yml 创建盒式磁带如错误消息中所述。

试试 VCR.use_cassette("wistia/upload", :record => :new_episodes) do ;这会将其记录为同一盒式磁带中的新请求。

https://www.relishapp.com/vcr/vcr/v/1-3-2/docs/record-modes 查看更多关于录像机记录模式的信息

关于ruby-on-rails - VCR::Errors::UnhandledHTTPRequestError 在使用 VCR 的 Controller 测试中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29101668/

相关文章:

ruby-on-rails - 如何在 Rails 中实现 Rouge 语法高亮?

ruby-on-rails - Rails 3.1 部署到 JBoss 4.2.2

ruby-on-rails - 升级Ruby版本和Rails兼容性

ruby-on-rails - VCR+rspec 元数据 : how to use inferred cassette name like use_vcr_cassette

ruby - 带 capybara -webkit 的录像机

ruby-on-rails - apache权限错误

ruby-on-rails - 用任意名称定义方法

ruby-on-rails - 是否可以在不运行测试套件的情况下重新录制 VCR 磁带?

ruby-on-rails - 如何编辑VCR gem返回的响应正文?