我有一些需要与Google Maps Routing API交互的Cucumber功能.我正在尝试使用VCR来消除这些交互.
我在我的功能中添加了一个VCR标签,如下所示:
@google_routing_api @javascript
Scenario: Creating a bus
Given I am on the buses page
When I follow "Get Started Now"
Run Code Online (Sandbox Code Playgroud)
然后添加了我的VCR配置 features/support/vcr.rb
require 'vcr'
VCR.config do |c|
# INFO: This is relative to the Rails.root
c.cassette_library_dir = 'features/fixtures/vcr_cassettes'
c.stub_with :fakeweb
end
# INFO: https://github.com/myronmarston/vcr/wiki/Usage-with-Cucumber
VCR.cucumber_tags do |t|
t.tag '@google_routing_api'
end
Run Code Online (Sandbox Code Playgroud)
但是当我举起我的傻瓜时,我被告知......
Real HTTP connections are disabled. Unregistered request: GET http://127.0.0.1:54181/__identify__
Run Code Online (Sandbox Code Playgroud) 我的应用与谷歌地图进行地理编码和Stripe付款.使用VCR我已经模拟了对这些服务的所有请求,这非常有效.但两者的库仍在加载javascript_include_tag.处理此问题的最佳方法是什么,以便集成测试可以完全脱离互联网?
您好,我正在尝试使用带有标签的 vcr 来测试黄瓜的谷歌身份验证。
一切都很顺利,直到令牌过期。我认为当它过期时会发生这种情况

但我有一个包含此内容的文件
http_interactions:
- request:
method: post
uri: https://accounts.google.com/o/oauth2/token
body:
Run Code Online (Sandbox Code Playgroud)
如果我允许录像机记录新的请求,该磁带的内容就会发生变化。我不明白为什么,如果方法和 uri 不将 POST 更改为https://accounts.google.com/o/oauth2/token。
我更改了标签来记录新剧集,现在测试正在通过......我一无所知。
我再次运行测试,现在当 POST 到 token url 时,我遇到了这个:
Completed 500 Internal Server Error in 449ms
Psych::BadAlias (Unknown alias: 70317249293120):
Run Code Online (Sandbox Code Playgroud) 我无法用录像机录制任何内容,我有这样的设置:
spec_helper.rb
require 'vcr'
VCR.configure do |c|
c.cassette_library_dir = 'spec/cassettes'
c.hook_into :webmock
c.configure_rspec_metadata!
c.default_cassette_options = { :record => :new_episodes }
end
Run Code Online (Sandbox Code Playgroud)
并测试:
describe SomeClass do
describe '#foo', vcr: true do
it 'should do http request', do
expect(subject.do_request).to be_true
end
end
end
Run Code Online (Sandbox Code Playgroud)
运行该规范会导致:
.HTTPI executes HTTP GET using the net_http adapter
SOAP request: (...)
Run Code Online (Sandbox Code Playgroud)
完全像没有录像机.遗憾的是文档中没有任何内容.我在这里报告了类似的问题,但要求httpi不会产生任何影响.要做到这一点应该怎么做?
我有一个规范文件,期望控制器操作将返回成功。
POST api/v1/users/:id/features/block控制器中的操作在外部API上调用了两个HTTP调用,唯一的不同在于主体。
我已将两个请求和响应放在同一VCR盒式磁带中,但是当使用盒式磁带时,只有第一个请求与之比较,而当第二个请求与第二个匹配时就会失败,从而导致测试失败。
我正在寻找一种使多个请求匹配的方法,以便控制器操作完成并成功返回。
我得到的错误是在结尾。
describe "POST /api/v1/users/:id/features/block" do
before(:each) do
@user = FactoryGirl.create(:user)
post :block, user_id: @user.id, block: "0"
end
it "should return 200 OK" do
expect(response).to be_success
end
end
Run Code Online (Sandbox Code Playgroud)
我的VCR配置和RSpec配置的简化版本如下:
VCR.configure do |c|
c.hook_into :webmock
c.default_cassette_options = {
match_requests_on: [:method, :uri, :body_regex]
}
c.register_request_matcher :body_regex do |request_1, request_2|
# Match body against regex if cassette body is "--ruby_regex /regexhere/"
if request_2.body[/^--ruby_regex\s*\//]
regex = request_2.body.gsub(/^--ruby_regex\s*\//, '').gsub(/\/$/, '')
request_1.body[/#{regex}/] ? true : false
else
true # No regex …Run Code Online (Sandbox Code Playgroud) 我想使用的一些第三方服务要求用户登录其网页。方便的 url 在下面的控制器中生成。用户去那里并在身份验证成功后返回我的服务
class MyController < App::BaseController
def login
redirect_to SOME_API.external_url(ENV['secret_id'])
end
end
Run Code Online (Sandbox Code Playgroud)
当用户返回我的服务时,他会在 URL 参数中带来一些来自第三方服务的反馈(例如:)myapp.com/login_callack?response=error&reason=wrong_password。这些参数有很多变体,因此我需要在操作中处理它们
def login_callback
#SOME MAGIC WITH COOKIES/ERRORS/What ever
redirect_to root_path
end
Run Code Online (Sandbox Code Playgroud)
我想使用 RSpec 和 capybara 为其编写功能规范。
scenario 'User authenticates with third-party thing' do
visit '/anything'
click_link 'Go to MyController#login and than get redirect to somerandom.url/login_logic'
# use cassette below
fill_out_form
click_button confirm
# magic happens on their side and their redirect user to my app into #login_callback action
expect(page).to have(/all fancy things i …Run Code Online (Sandbox Code Playgroud) 我们使用 VCR 来记录 Rails 应用程序中测试的 http 请求。这些磁带目前已提交来源,但 CircleCI 似乎并未使用它们,因为 CircleCI 每次都会提出新的请求。测试正在本地环境中通过。我希望 CircleCI 使用保存在我们的 git 存储库中的磁带。这可能吗 ?
当我在 CircleCI 服务器上执行测试时,测试通过了。
任何指导或建议将不胜感激!
本地环境:mac OS Big sur 11.6.1
gemfile.rb
gem 'rspec-rails', '~> 5.0'
gem 'vcr', '~> 6.1'
Run Code Online (Sandbox Code Playgroud)
录像机
VCR.configure do |config|
config.cassette_library_dir = 'spec/cassettes'
config.hook_into :webmock
config.ignore_localhost = true
config.configure_rspec_metadata!
config.filter_sensitive_data('<BEARER_TOKEN>') do |interaction|
auths = interaction.request.headers['Authorization']&.first
if auths && (match = auths.match(/^Bearer\s+([^,\s]+)/))
match.captures.first
end
end
end
Run Code Online (Sandbox Code Playgroud) My specs will pass in master branch. If I create a new branch and modify some code completely unrelated to the subscriptions, they'll fail with this. The only way I can get them to pass is to change my vcr.rb to have :record => :new_episodes.
If I leave that option on, then almost every time my specs run I have new modified data files for cassettes that end up being committed which really dilute the logs for Git.
Any …
我已经设置了VCR,并且可以在我编写的无问题的多个测试上运行。我尝试写入的最新测试将在首次运行时通过,但在此之后仍将失败,除非删除卡带。该测试的代码是:
it "doesn't blow up if a user doesn't have billing info" do
VCR.use_cassette('tax_reconciler/no_method_error')do
user_guid = rand(10000000)
CreateRecurlyTestData.create_account(user_guid, nil, nil)
tax_reconciler = TaxReconciler.new
new_tax_amount = rand(100000)
user = create_test_user(:guid => user_guid)
expect(tax_reconciler.update_tax_amount(user, new_tax_amount)).to_not raise_error
end
end
Run Code Online (Sandbox Code Playgroud)
错误消息如下:
失败/错误:CreateRecurlyTestData.create_account(user_guid,nil,nil)VCR ::错误:: UnhandledHTTPRequestError:
================================================================================
An HTTP request has been made that VCR does not know how to handle:
GET https://2b64d08ef45c446dbba75720a37b7d41:@api.recurly.com/v2/accounts/3276643
VCR is currently using the following cassette:
- /Users/Evan/dish/stallone/fixtures/vcr_cassettes/tax_reconciler/no_method_error.yml
- :record => :once
- :match_requests_on => [:method, :uri]
Under the current configuration VCR …Run Code Online (Sandbox Code Playgroud) 我正在通过Google Drive Ruby gem使用Google Drive API并使用VCR来记录请求。
我正在通过 JWT 进行身份验证,并希望过滤掉 JWT 请求和返回的不记名令牌。
由于我不知道 Google 在运行时给我的 JWT 令牌或不记名令牌,因此我无法使用filter_sensitive_data. 因此,在测试运行后,我需要过滤以下乱七八糟的代码,以便对我的磁带进行消毒:
after(:each) do |example|
# Filter out JWT and bearer tokens from requests
if VCR.current_cassette.recording?
interactions = VCR.current_cassette.new_recorded_interactions
# Remove JWT token
interactions.first.request.body.gsub! /(?<=assertion\=).*/, '<JWT_TOKEN>'
# Get and replace access token from body
body = JSON.parse(interactions.first.response.body)
access_token = body['access_token']
body['access_token'] = '<ACCESS_TOKEN>'
interactions.first.response.body = body.to_json
# Replace access token in each auth request
interactions.drop(1).each do |i|
i.request.headers['Authorization'][0].gsub!(access_token, '<BEARER_TOKEN>') …Run Code Online (Sandbox Code Playgroud)