如何使用 VCR


继之前的 [测试中怎么 stub 外部服务](http://qiita.com/xiangzhuyuan/items/9f3301cd30e09369e7d1) 再来看看怎么使用 `VCR`


看看之前的配置,主要发生在` spec/spec_helper.rb`.
之前已经配置了`webmock` 来禁用外部网络访问,然后通过一个假的的基于` sinatra` 的服务来 mock 对于 api 的访问.

文章说到了 vcr, 但是没有说清楚怎么用,现在我们再来看看.
首先还是在` spec_helper.rb` 中配置` vcr`.

```ruby
require 'vcr'

...

VCR.configure do |c|
  c.cassette_library_dir = 'spec/support/vcr' #录制脚本存放的位置
  c.hook_into :webmock 
end


...

```

然后我们再追加一个测试,下面的志这样子:

```ruby
require 'spec_helper'

feature 'External request' do
  it 'queries FactoryGirl contributors on GitHub' do
    uri = URI('https://api.github.com/repos/thoughtbot/factory_girl/contributors')

    response = JSON.load Net::HTTP.get(uri)

    # expect(response).to be_an_instance_of(String)
    expect(response.first['login']).to eq 'joshuaclayton'

  end
  it 'another api request' do
    uri = URI('https://api.twitter.com/1.1/statuses/mentions_timeline.json') #因为没有认证,返回的结果就是一个 error 信息.
    response = JSON.load Net::HTTP.get(uri)

    expect(response['error'].first['message']['code']).to eq '215'
  end
end

```
然后我们在命令行执行` bundle exec rspec` 你会发现原来的那个 api stub 是好用的,而新追加的 api 访问会报错, 下面这样子的:

```ruby

Failures:

  1) External request another api request
     Failure/Error: response = JSON.load Net::HTTP.get(uri)
     VCR::Errors::UnhandledHTTPRequestError:


       ================================================================================
       An HTTP request has been made that VCR does not know how to handle:
         GET https://api.twitter.com/1.1/statuses/mentions_timeline.json

       There is currently no cassette in use. There are a few ways
       you can configure VCR to handle this request:

         * 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].
           
           > 如果你不明白vcr 怎么搞这个请求而报错,你可以通过` debug_logger` 来记录更多的信息
           
         * If you want VCR to record this request and play it back during future test
           runs, you should wrap your test (or this portion of your test) in a
           `VCR.use_cassette` block [2].
           
           > 如果你希望让 VCR 来录制这个请求,然后在之后的测试中回放,通过这个` VCR.use_cassette` 来处理.
           
         * If you only want VCR to handle requests made while a cassette is in use,
           configure `allow_http_connections_when_no_cassette = true`. VCR will
           ignore this request since it is made when there is no cassette [3].
           
           > 如果没有 cassette, 就设置这个来跳过这个处理 allow_http_connections_when_no_cassette = true
           
         * If you want VCR to ignore this request (and others like it), you can
           set an `ignore_request` callback [4].
           
           > 如果不想处理,就直接忽略了 ignore_request

       [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/getting-started
       [3] https://www.relishapp.com/vcr/vcr/v/2-9-3/docs/configuration/allow-http-connections-when-no-cassette
       [4] https://www.relishapp.com/vcr/vcr/v/2-9-3/docs/configuration/ignore-request
       ================================================================================
     # ./spec/features/external_request_spec.rb:15:in `block (2 levels) in <top (required)>'

```


也就是说这个情况下,对于` twitter api` 的访问被` VCR` 截住了.怎么办呢,看他的提示.