首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用VCR的控制器测试中的VCR::Errors::UnhandledHTTPRequestError

使用VCR的控制器测试中的VCR::Errors::UnhandledHTTPRequestError
EN

Stack Overflow用户
提问于 2015-03-17 22:25:57
回答 1查看 10.1K关注 0票数 6

我正在尝试模拟一个api调用,在这个调用中,我上传了一个文件,我使用的是Wistia Upload API,因为我不想在每次测试时都访问服务器,所以我第一次尝试录像机。

我的spec/controllers文件夹中有以下测试:

代码语言:javascript
复制
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调用在模型回调中触发,如下所示:

代码语言:javascript
复制
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

我已经调试了对此方法的调用,并且可以正确地调用它,但是当我运行我的测试时,我得到以下错误:

代码语言:javascript
复制
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也没有处理该请求。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-17 22:58:01

磁带正在/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

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29101668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档