1.在Gemfile增加VCR gem

git diff Gemfile

得到结果:

+gem "vcr"

2.在单元测试使用VCR进行发送http请求

(1)在test/test_helper.rb增加:

VCR.configure do |config|
  config.cassette_library_dir = "vcr_fixtures/vcr_cassettes"
  config.hook_into :webmock
  config.default_cassette_options[:match_requests_on] = [:method, :path, :body]
end

(2)在单元测试增加:

 test "should get transaction_fees, for pending tx" do
-        get transaction_fees_api_v2_statistics_url
-        data = JSON.parse(response.body)
-        assert_equal 2, data['pending_transaction_fee_rates'].size
-        assert data['pending_transaction_fee_rates'].first['fee_rate'] > 0
-
-        assert_response :success
+        VCR.use_cassette("get transaction_fees, for pending tx") do
+          get transaction_fees_api_v2_statistics_url, headers: { "Content-Type": "application/vnd.api+json", "Accept": "application/json" }
+          data = JSON.parse(response.body)
+          assert_equal PoolTransactionEntry.all.size, data['transaction_fee_rates'].size
+          assert data['pending_transaction_fee_rates'].first['fee_rate'] > 0
+
+          assert_response :success
+        end
       end

(3)单元测试的方法会生成这样的yml文件:

    vcr_fixtures/vcr_cassettes/get_post.yml
    vcr_fixtures/vcr_cassettes/get_transaction_fees.yml
    vcr_fixtures/vcr_cassettes/get_transaction_fees_for_committed_tx.yml
    vcr_fixtures/vcr_cassettes/get_transaction_fees_for_last_n_days_transaction_fee_rates.yml
    vcr_fixtures/vcr_cassettes/get_transaction_fees_for_pending_tx.yml

查看第一个文件:

---
http_interactions:
- request:
    method: get #使用了get请求
    uri: http://www.example.com/api/v2/statistics/transaction_fees
    body:
      encoding: US-ASCII
      string: ''
    headers:
      Accept-Encoding:
      - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
      Accept:
      - "*/*"
      User-Agent:
      - Ruby
      Host:
      - www.example.com
  response:
    status:
      code: 404
      message: Not Found
    headers:
      Cache-Control:
      - max-age=604800
      Content-Type:
      - text/html; charset=UTF-8
      Date:
      - Mon, 13 Mar 2023 12:39:52 GMT
      Expires:
      - Mon, 20 Mar 2023 12:39:52 GMT
      Server:
      - EOS (vny/0453)
      Vary:
      - Accept-Encoding
      Content-Length:
      - '648'

查看最后一个pending的文件:

---
http_interactions:
- request:
    method: post #使用了post请求
    uri: http://localhost:8114/
    body:
      encoding: UTF-8
      string: '{"id":1,"jsonrpc":"2.0","method":"get_transaction","params":["0xe9772bae467924e0feee85e9b7087993d38713bd8c19c954c4b68da69b4f4644"]}'
    headers:
      Accept-Encoding:
      - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
      Accept:
      - "*/*"
      User-Agent:
      - Ruby
      Content-Type:
      - application/json
      Connection:
      - keep-alive
      Keep-Alive:
      - '30'
  response:
    status:
      code: 200
      message: OK
    headers:
      Content-Type:
      - application/json; charset=utf-8
      Content-Length:
      - '1341'
      Date:
      - Tue, 14 Mar 2023 01:14:00 GMT
    body:
      encoding: UTF-8
      string: '{"jsonrpc":"2.0","result":{"transaction":{"cell_deps":[{"dep_type":"dep_group","out_point":{"index":"0x0","tx_hash":"0x71a7ba8fc96349fea0ed3a5c47992e3b

 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;<br />
&nbsp; recorded_at: Tue, 14 Mar 2023 01:14:00 GMT<br />
- request: