You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSCode中如何跳转到Ruby/RSpec的shared_examples定义?

解决VSCode中RSpec it_should_behave_like 跳转定义失效的问题

方法1:换用Ruby LSP扩展

Solargraph对RSpec的DSL支持不够到位,Shopify维护的官方Ruby LSP对RSpec语法的理解更全面:

  • 卸载已安装的Ruby Solargraph和VSCode Ruby扩展
  • 在VSCode扩展商店安装Ruby LSP
  • 重启VSCode后,再试F12跳转it_should_behave_like到对应的shared_examples定义

方法2:强化Solargraph的RSpec支持

不想换扩展的话,按以下步骤优化:

  • 终端执行gem update solargraph,把Solargraph更到最新版
  • 安装Solargraph的RSpec插件:gem install solargraph-rspec
  • 打开VSCode设置(Ctrl+,),搜索solargraph.plugins,把rspec加入插件列表,改完重启VSCode
  • 项目根目录如果没有.solargraph.yml,就执行solargraph config生成,然后在文件里添加spec目录路径:
    include:
      - 'spec/**/*.rb'
    

方法3:手动让Solargraph索引RSpec文件

要是上面的方法都不管用,就强制Solargraph索引你的spec目录:

  • 在VSCode设置里,确认solargraph.diagnostics是开启状态,同时把solargraph.include设为["spec/**/*.rb"]
  • 终端执行solargraph bundle,让Solargraph重新扫描并索引项目里的所有Ruby文件,包括RSpec的shared_examples

内容的提问来源于stack exchange,提问作者Bart Vandewoestyne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 13:17:15