我正在使用录像机记录我正在整合的其他系统的响应.
但是,这个响应获得了一个巨大的JSON,VCR正在以二进制格式保存它:
body:
encoding: ASCII-8BIT
string: !binary |-
eyJsaXN0IjpbXSwiZmFjZXRzIjpbeyJuYW1lIjoiU2FsZXNDaGFubmVsTmFt
ZSIsInR5cGUiOi...
Run Code Online (Sandbox Code Playgroud)
有没有办法我只能将响应体保存为JSON?
我想这样做来编辑返回的JSON,以便为我的测试制作其他场景,
谢谢
我为一个REST api开发了一个Ruby接口.我使用rspec和vcr进行测试.
所有测试都使用专门为其创建的帐户凭据.
我无法决定:将我的测试(vcr磁带)的缓存响应存储在存储库中,或允许用户和订阅者编写自己的磁带是否正确?
以皇帝的名义!让圣战开始吧!
我正在添加一个rspec钩子,它允许我打开vcr并使用当前示例的名称作为卡带名称.
it "should have collaborators", :vcr => :once do
# web interactions
end
config.around(:each, :vcr => :once) do |example|
VCR.use_cassette(example.name, :record => :once) do
example.call
end
end
Run Code Online (Sandbox Code Playgroud)
麻烦的是我不知道如何获取当前示例的名称(example.name不起作用).
我正在编写一个API包装器作为gem,我想使用RSpec测试API响应.
这个问题是所有的API请求都是使用GET生成的,并且在url中包含一个API密钥:
例如 game/metadata/{api_key}
这给测试带来了问题,因为我不想将API密钥保存在git存储库历史记录中.有什么方法可以进行这些规范测试,最好使用RSpec/VCR,而不是将API密钥存储在版本控制中?
我尝试过使用环境变量,但VCR仍然存储整个请求,而不仅仅是响应体.
我们用C#和.net编写了一个基本的网络应用程序.对于测试,我们喜欢使用类似于Ruby的VCR.C#/ .net是否存在这样的事情?
我想用 VCR 测试 invoice.payment_failed 事件
伪代码:
在步骤 1 中,Stripe 返回错误,提示我要添加无效卡。我使用了来自https://stripe.com/docs/testing 的“4000000000000119”卡号
基本上我想创建一个场景,其中用户拥有在他们第一次购买/订阅时曾经有效的卡。但一年后它不再有效。
我不想模拟事件数据并将它们存储在 yml 中。我直接从 Stripe 获取事件并用 VCR 记录它。
答案是: 4000 0000 0000 0341 卡号。在https://stripe.com/docs/testing 中描述 (所以在发布后 8 小时内不允许我回答我的问题)
我一直在寻找各处,我尝试了节点重播,但是使用量角器,但它不适用于硒.
我也试过vcr.js和棕褐色.
我如何设置我的测试,他们进行初始调用,但将它们存储为像vcr一样的磁带.
干杯.
我正在尝试访问这样的 pypi 网址:
PYPI_URL = https://pypi.org/pypi/Django/json
try:
response = requests.get(PYPI_URL)
except requests.exceptions.RequestException as err:
print("Unable to access Pypi. ", err)
Run Code Online (Sandbox Code Playgroud)
但是当我尝试为 pytest 生成录像带时,出现此错误。
E AssertionError: assert 'Unable to access Pypi' not in "Unable to acce...l.c:1108)')))\n"
E 'Unable to access Pypi' is contained here:
E Unable to access Pypi. Error Connecting: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /pypi/Django/json (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1108)')))
E ? +++++++++++++++++++++
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?为什么只有当我尝试生成盒式磁带时它才会抱怨?如果我只运行 pytest 而不生成 VCR 磁带,测试就会通过。
情况:使用Rspec,FactoryGirl和VCR测试rails应用程序.
每次创建用户时,都会通过Stripe的API创建关联的Stripe客户.在测试时,添加一个VCR.use_cassette或describe "...", vcr: {cassette_name: 'stripe-customer'} do ...每个涉及用户创建的规范都没有意义.我的实际解决方案如下:
RSpec.configure do |config|
config.around do |example|
VCR.use_cassette('stripe-customer') do |cassette|
example.run
end
end
end
Run Code Online (Sandbox Code Playgroud)
但这是不可持续的,因为每个http请求都会使用相同的盒式磁带,这当然非常糟糕.
问题:如何根据个人要求使用特定灯具(磁带),而无需为每个规格指定磁带?
我有类似的东西,伪代码:
stub_request(:post, "api.stripe.com/customers").with(File.read("cassettes/stripe-customer"))
Run Code Online (Sandbox Code Playgroud)
相关的代码片段(作为要点):
# user_observer.rb
class UserObserver < ActiveRecord::Observer
def after_create(user)
user.create_profile!
begin
customer = Stripe::Customer.create(
email: user.email,
plan: 'default'
)
user.stripe_customer_id = customer.id
user.save!
rescue Stripe::InvalidRequestError => e
raise e
end
end
end
# vcr.rb
require 'vcr'
VCR.configure do |config|
config.default_cassette_options = { record: :once, re_record_interval: 1.day …Run Code Online (Sandbox Code Playgroud) 我有一个带有用户模型的仅限 api 的 RoR 应用程序。用户通过 Twilio/Authy(使用这个gem)进行身份验证。has_one authy_user用于存储认证信息的每个用户模型,带有dependent: :destroy.
该authy_user模型有一个before_destroy钩子,它通过 authy gem 连接到 authy api 并删除那里的用户。
该authy_user规范运行蛮好的,我录的磁带两种登记并删除与authy API用户。
一块的authy_user规格:
describe "delete user" do
before do
@user = create(:user_with_authy_user)
end
context "user is registered" do
it "deletes user in authy and locally" do
VCR.use_cassette("authy_delete_user") do
expect {
@user.authy_user.destroy
}.to change { AuthyUser.count }.by(-1)
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
(这个失败,出现相同的错误,如果我从改变它下面@user.authy_user.destroy到@user.destroy)
我的问题是user …