Rails中Capybara.using_session与debug导致RSpec套件执行后挂起
Capybara+RSpec测试套件执行后挂起问题解决
环境信息
- Rails 7.0.4
- RSpec 3.11.0
- Capybara 3.37.1
- selenium-webdriver (gem) 4.5.0
- Gemfile中引入Ruby-debug:
gem 'debug', platforms: %i[ mri mingw x64_mingw ]
问题现象
在测试中添加Capybara.using_session调用后,所有测试用例执行通过,但RSpec测试套件会在执行完成后出现挂起现象。无论将Capybara默认驱动设置为:selenium_chrome_headless还是:selenium_chrome,该问题都会复现。
复现测试代码
require 'rails_helper' describe "client app", type: :feature do describe "when starting the experience", type: :feature do # TODO: figure out why capy is hanging here it "can load the home page" do # THIS CAUSES AFTER-RSPEC HANG even though all specs pass Capybara.using_session("client session") do visit "/" end end it "loads with a client id" do visit '/' end end end
已隔离的必要条件
- 使用
Capybara.using_session且块内调用visit访问页面(移除其中任一操作,问题消失) - 同一测试套件中其他测试用例也存在
visit操作(移除该操作,问题消失) - 引入
debuggem(从Gemfile中移除该gem,问题消失)
排查情况
将出现问题的应用精简至最小程度后,仍能复现该挂起现象,但在新创建的配置完全相同的Rails应用中,却无法复现,推测存在未发现的第四影响因素。
问题解决(2022-12-15)
经确认,问题源于debug gem与Capybara多会话管理的兼容性冲突。可行解决方式包括:
- 临时规避:从Gemfile中移除
debuggem,测试套件即可正常结束,无挂起现象。 - 彻底修复:升级
debuggem至兼容版本,或在Capybara测试场景中禁用debuggem的相关钩子。
内容的提问来源于stack exchange,提问作者Jason FB
相关产品推荐
相关产品推荐

