我目前正在使用RSpec2,Cucumber和VCR(通过WebMock),一切都运行良好.
通常使用VCR记录所有请求,然后根据录制的磁带重放.
现在我想在某些情况下允许真正的Web请求:
@live.对于这些测试 - 仅限这些测试 - 我想允许真正的Web请求.我正在开发的一个项目有集成测试,它实际上已经通过线路击中了第三方API ...运行这些测试需要很长时间.我建议我们实现像VCR这样的东西,以便测试与之交互的数据可以作为固定装置捕获,并提高这些测试的可靠性和速度.
我此刻无法访问代码库,但我相信测试是这样做的:
before do
login_some_user
end
after do
make_web_request_to_clear_items_in_cart
end
it "adds some items to the user's cart" do
make_web_request_to_add_item_to_a_cart
end
Run Code Online (Sandbox Code Playgroud)
所以基本上前面的块是发出一个Web请求,例子是一个完全不同的请求,一个后块(我知道它不是理想的)发出第三个请求来清理由示例创建的记录.
我在spec_helper中设置了一个around块,用于捕获Web请求并以示例的名称存储它们.但是,在重复运行测试后,我发现它们变得非常松散,有时会通过,有时不会......我尝试在单独的VCR.use_cassette块调用中包装前后块,但它没有任何区别.
我想知道我是否遗漏了一些东西,如果有办法处理这样的多个请求或什么?
我正在尝试模拟一个在其中上传文件的api调用,我正在使用Wistia Upload API,并且由于我不想每次测试都击中服务器,因此我是第一次尝试VCR。
我的spec / controllers文件夹中有以下测试:
let(:file) { Rack::Test::UploadedFile.new("video_path", 'video/mp4') }
describe "GET #index" do
it "assigns all videos as @videos" do
VCR.use_cassette "wistia/upload" do
video = Video.create! valid_attributes
get :index, {}
expect(assigns(:videos)).to eq([video])
end
end
end
Run Code Online (Sandbox Code Playgroud)
结束
在模型回调中触发API调用,如下所示:
class Video < ActiveRecord::Base
after_save :move_video
def move_video
uri = URI('https://upload.wistia.com/')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
# Construct the request.
request = Net::HTTP::Post::Multipart.new uri.request_uri, {
'api_password' => '',
'project_id' => ''
'file' => my_video_file
}
# Make …Run Code Online (Sandbox Code Playgroud) 我正在访问一个 API,对于每个请求,盒式 YAML 文件中都有一个记录的请求/响应。但是,请求之间的唯一区别在于id查询参数。
如何缩小 YAML 文件以便为每个请求动态生成 URL?
我正在使用Rspec与Capybara运行使用VCR gem的集成测试来模拟存根api请求.我面临着各种奇怪的问题.测试第一次成功运行并创建yaml文件以保存请求及其响应.但是如果我在创建yaml文件后再次运行测试,那么它会遇到问题并显示以下问题: -
F
Failures:
1) AffiliateSignUpProcesses Sign up using affiliate link should create User/Affiliate/AffiliateToken and Conversion when user is created
Failure/Error: visit pricing_path(:aid => 'this_is_sample_value')
VCR::Errors::UnhandledHTTPRequestError:
================================================================================
An HTTP request has been made that VCR does not know how to handle:
GET http://127.0.0.1:58021/__identify__
VCR is currently using the following cassette:
- /home/pcname/appname/selenium-error/fildername/appaname/cassettes/acceptanceTest/AffiliateSignUpProcess.yml
- :record => :once
- :match_requests_on => [:method, :uri]
Under the current configuration VCR can not find a suitable HTTP interaction
to replay and is prevented from …Run Code Online (Sandbox Code Playgroud) rspec ruby-on-rails-3 capybara-webkit selenium-webdriver vcr
在所有带有请求的规范中,example.com我想忽略URI中关于请求匹配器的尾随id.像这样的东西.
VCR.configure do |c|
# omitted
c.register_request_matcher :uri_ignoring_trailing_id do |request_1, request_2|
# omitted
end
c.before_http_request(lambda { |req| req.uri =~ /example.com/ }) do
c.default_cassette_options = { match_requests_on: [ :uri_ignoring_trailing_id ] }
end
end
Run Code Online (Sandbox Code Playgroud) 我有一个Rails 4应用程序,它使用自定义身份验证gem来根据第三方API对用户进行身份验证。该应用程序要求对网站上的大多数操作进行身份验证(访问者可以做的很少)。
我正在尝试使用VCR记录所有集成测试在身份验证期间提出的api请求,但是我可以在SO和Relish文档上找到的所有示例仅涵盖如何在'describe do'规范中使用Rspec进行此操作,如此处所引用:
https://www.relishapp.com/vcr/vcr/v/1-6-0/docs/test-frameworks/usage-with-rspec
由于没有客户参与该项目,因此我正在使用Rspec和Capybara而不是Cucumber编写集成测试,因此我的测试使用的是“功能/场景”格式,如下所示:
feature 'posts' do
scenario 'a user can log in' do
# use vcr for api request
sign_in_user # refers to a method that handles the api call to log in a user, which is what I would like VCR to record.
expect(page).to have_content("User signed in successfully")
end
end
Run Code Online (Sandbox Code Playgroud)
使用文档中描述的命令:
use_vcr_cassette
Run Code Online (Sandbox Code Playgroud)
在“方案”块中,返回错误:
Failure/Error: use_vcr_cassette
undefined local variable or method `use_vcr_cassette' for #<RSpec::ExampleGroups::Posts:0x007fb858369c38>
Run Code Online (Sandbox Code Playgroud)
我按照文档在我的spec / rails_helper.rb(包含在spec / spec_helper.rb中)中设置了VCR ...基本上看起来像这样:
require 'vcr'
VCR.configure do |c|
c.cassette_library_dir = …Run Code Online (Sandbox Code Playgroud) 如何告诉VCR我希望它完全忽略spec文件?
我在Google网上论坛上发布了一条帖子,建议允许真正的HTTP请求,或明确关闭VCR.
在我看来,除非规范具有:vcr元数据标签,否则VCR不会嗤之以鼻.我不想关闭VCR并重新打开before/ after,因为我不知道它是否已预先打开.我不想在所有规范中允许真正的HTTP请求,只是某些特定的规范.
有没有办法让VCR更具选择性?
我正在使用VCR gem来模拟HTTP查询.我录了录音带,但后来我不得不改变一些东西,现在我收到一个错误:
An HTTP request has been made that VCR does not know how to handle:
POST http://api.endpoint.here/path.json
Run Code Online (Sandbox Code Playgroud)
现在,因为它是一个POST请求,VCR配置为匹配正文和路径上的那些.我可以记录或转储未处理请求的正文,以便我可以相应地调整磁带吗?谢谢.
我的 Rails 项目中有很多 RSpec 测试,用于测试对外部 REST API 的 HTTP 调用,并使用 VCR 磁带记录请求和响应。目前我的 VCR 配置如下:
VCR.configure do |c|
c.cassette_library_dir = 'spec/vcr_cassettes'
c.hook_into :webmock
c.configure_rspec_metadata!
end
Run Code Online (Sandbox Code Playgroud)
因此请求匹配规则仅匹配 HTTP 方法和 URI。我想更改此设置以匹配请求正文:
VCR.configure do |c|
c.cassette_library_dir = 'spec/vcr_cassettes'
c.hook_into :webmock
c.configure_rspec_metadata!
c.default_cassette_options = {
:match_requests_on => [:uri, :method, :body],
}
end
Run Code Online (Sandbox Code Playgroud)
但由于我的项目中已经有很多测试,我想逐步进行,仅对某些测试激活此新限制,这样其他测试(使用旧磁带)就不会中断。
有没有办法将参数传递给 RSpec 测试,以便仅针对某些特定测试或测试组制定自定义请求匹配规则?
我想象类似的事情
it 'reverts transaction', :vcr, :body_matching => true do
# something
end
Run Code Online (Sandbox Code Playgroud)
然后根据body_matching参数动态更改设置。
我正在使用带有RSpec的VCR对一段API代码进行单元测试,我遇到了一个小小的挑战.
我的客户端代码用于sleep对API请求进行速率限制.感谢这个优雅的解决方案,我现在可以sleep使用单元测试中的存根拦截消息:
expect(subject).to receive(:sleep)
Run Code Online (Sandbox Code Playgroud)
问题是,当我开发这个代码时,我必须经常删除我的磁带,但是因为我已经存根sleep,所以对真正的API请求没有速率限制并且规范爆炸了.
我认为我想做的是以下内容:
expect(subject).to receive(:sleep) if !vcr.real_request?
Run Code Online (Sandbox Code Playgroud)
因此,sleep当我录制磁带时,我不会干扰主题.
我看到有趣的相关讨论,但它似乎是一个不同的情况,我不知道如何直接利用它.但是,这确实让我发现了VCR挂钩(特别是before_playback),但我不清楚如何导出subject到块的范围内,或者代理块执行到我的测试范围内来制作存根.
有什么想法吗?
注意:我正在使用我的config.configure_rspec_metadata!功能spec_helper.rb自动捕获/命名我的磁带,如果这有任何区别(即我没有明确地将每个单独的API方法包装在它自己的VCR块中).