如何解决带有 RSPEC 的 VCR 抛出一个 JSON 文本必须至少包含两个八位字节
我在 Ruby 2.4.10 项目上使用 VCR 3.0.3,但不知何故我的 VCR 测试无法正常工作。我不断收到错误
A JSON text must at least contain two octets!
这是我的 vcr.rb 配置
VCR.configure do |config|
config.cassette_library_dir = Rails.root.join("spec","fixtures/vcr_cassettes")
config.hook_into :webmock
config.allow_http_connections_when_no_cassette = true
config.filter_sensitive_data('<api_key>') { ENV['WAVECELL_ACC_API_KEY'] }
config.ignore_localhost = true
end
这是我的/spec/services/text_message_service_spec.rb
require 'rails_helper'
describe TextMessageService do
before(:each) { create(:text_message_type) }
let(:user) { create(:user) }
let(:message) { "This is a test message. Welcome to Wavecell" }
let(:service) { TextMessageService.new(user,message) }
context "Text Message is Sent" do
it 'should have text message record increase by 1' do
user.phone_num = "60120000001"
user.save
VCR.use_cassette('wavecell',:record => :new_episodes) do
expect { service.send('ver_code') }.to change(user.text_messages,:count).by(1)
end
end
end
end
我已经有一个录音带,我希望 vcr 使用它,但它似乎没有使用它
wavecell.yml
---
http_interactions:
- request:
method: post
uri: https://sms.8x8.com/api/v1/subaccounts/test_1/messages
body:
encoding: UTF-8
string: '{"destination":"60120000001","text":"This is a test message. Welcome to Wavecell","source":"test","encoding":"AUTO"}'
headers:
Accept:
- application/json
User-Agent:
- rest-client/2.1.0 (darwin20.1.0 x86_64) ruby/2.4.10p364
Authorization:
- Bearer <api_key>
Content-Type:
- application/json
Content-Length:
- '96'
Accept-Encoding:
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
response:
status:
code: 200
message: OK
headers:
Date:
- Wed,03 Mar 2021 12:48:17 GMT
Content-Type:
- application/json; charset=utf-8
Content-Length:
- '201'
Connection:
- keep-alive
Cache-Control:
- no-store
vary:
- Authorization
X-Request-Id:
- 4be43c43-19b4-4afa-a198-ace000d304f0
body:
encoding: UTF-8
string: '{"umid":"4be43c43-19b4-4afa-a198-ace000d304f0","clientMessageId":null,"destination":"60120000001","encoding":"GSM7","status":{"code":"QUEUED","description":"SMS
is accepted and queued for processing"}}'
http_version:
recorded_at: Wed,03 Mar 2021 12:48:17 GMT
我的确切服务代码在这里 text_message_service.rb
class TextMessageService
require 'uri'
require 'rest-client'
include HTTParty
def initialize(user,message)
@user = user
@message = message
end
# @author Tharan
# @note this method sends the sms to the user and creates a log in text_messages table
def send(type)
type_id = TextMessageType.find_by(short_name: type)
response = send_with_wavecell
create_text(type,'wavecell',response)
end
private
def send_with_wavecell
url = "https://sms.8x8.com/api/v1/subaccounts/#{ENV['WAVECELL_SUB_ACC_ID']}/messages"
headers = {
"Authorization": "Bearer #{ENV['WAVECELL_ACC_API_KEY']}","Content-Type": "application/json","Accept": "application/json"
}
payload = {
"destination": @user.no_plus_phone_num,"text": @message,"source": "test","encoding": "AUTO"
}
json_response = nil
begin
RestClient.post(url,payload.to_json,headers) do |response|
json_response = JSON.parse(response.body)
end
rescue => exception
json_response = {"message": exception,"exception_error": true}.stringify_keys
Raven.capture_exception(exception)
end
return json_response
end
end
当我在本地机器上运行测试时,它运行良好,但在圆 ci 上,它一直失败并显示错误 A JSON text must at least contain two octets!
。当它在circle ci上运行时应该使用我录制的盒式磁带时,它也感觉像是一个新的http请求。
我该如何解决这个问题?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。