如何在VSCode中使RSpec与调试器IDE一起使用

时间:2020-05-22 18:08:26

标签: ruby-on-rails visual-studio-code rspec vscode-debugger

尽管花了大约三个小时的时间来使此工作正常进行,但我无法在获得RSpec的整个生命期内与VSCode上的调试器一起使用。我可以让Rspec在VSCode上的 terminal 中运行,但这并没有为我提供任何IDE的调试和调试功能。

这是我在launch.json文件中得到的:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Run RSpec - all",
            "type": "Ruby",
            "request": "launch",
            "cwd": "${workspaceRoot}",
            "program": "/Users/my-home-dir/.rvm/gems/ruby-2.6.5@project-gemset-name/wrappers/rspec",
            "pathToRDebugIDE": "/Users/my-home-dir/.rvm/gems/ruby-2.6.5@project-gemset-name/gems/ruby-debug-ide-0.7.2",
            "args": [
                "--pattern",
                "${workspaceRoot}/spec/**/*_rspec.rb"
            ]
        }
    ]
}

我的gemfile包含:

  gem 'ruby-debug-ide', '~>0.7.2'
  gem 'debase', '~>0.2.4.1'

我感觉到错误可能是由于RVM和VSCode不兼容而引起的,但是我不知道如何解决该问题。

所有操作均按照此处的Microsoft食谱进行:https://github.com/Microsoft/vscode-recipes/tree/master/debugging-Ruby-on-Rails

每次运行此安装程序时,在调试控制台中都会出现以下错误:

Debugger terminal error: Process failed: spawn rdebug-ide ENOENT

有什么办法可以运行它?还有什么办法让它使用Guard以便它自动运行?

2 个答案:

答案 0 :(得分:1)

我知道了。不幸的是,我没有使用RVM。因此,我的解决方案涉及rbenv。无论如何,我都会在这里分享它,以防它对您或其他人有帮助。

which rspec向我指出了rbenv用于执行在当前Ruby版本下安装的rspec版本的shim(shell脚本)。当我使用该路径配置launch.json时,rdebug-ide不喜欢填充程序。我认为它正在期待可执行文件。

因此,我运行了rbenv which rspec并获得了可执行文件的实际路径。将其插入launch.json后,它就可以正常工作。当然,如果更改正在运行的Ruby版本,则必须更新文件以指向在新版本的Ruby下安装的RSpec的版本。

鉴于社区中Ruby版本管理器的普遍性,我认为ruby-debug-ide会考虑这一点。也许值得在他们的GitHub上解决一个问题:https://github.com/ruby-debug/ruby-debug-ide

答案 1 :(得分:0)

我添加了一个 preLaunchTask 来启动 rdebug-ide 服务器,然后在连接调试器之前寻找模式。

我使用焦点标志来控制运行或不运行哪些测试。能够在主脚本文件和 *_spec.rb 文件中设置断点。

宝石文件:

group :test, :development do
  gem "ruby-debug-ide", "~> 0.7.2"
  gem "debase", "~> 0.2.4"
end

launch.json:

{     
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for rdebug-ide rspec",
            "type": "Ruby",
            "request": "attach",
            "remoteHost": "127.0.0.1",
            "remotePort": "1234",
            "preLaunchTask": "run-rdebug-for-rspec",
            "remoteWorkspaceRoot": "${workspaceRoot}",
            "cwd": "${workspaceFolder}"
        }
    ]
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [{
        "label": "run-rdebug-for-rspec",
        "command": "bundle",
        // could update these are you see fit 
        // my tests were located in /spec folder
        "args": [
          "exec","rdebug-ide",
          "--host","0.0.0.0",
          "--port","1234",
          "--dispatcher-port","26162",
          // needed to specify the full path of bundle
          // can be found by `which bundle`
          "--", "/usr/local/bin/bundle", "exec", "rspec", "spec/"
        ],
        "type": "shell",
        "isBackground": true,
        // this will look for a pattern to attach to rdebug server
        // attaching to the server will start running the tests
        "problemMatcher": [
            {
              "pattern": [
                {
                  "regexp": ".",
                  "file": 1,
                  "location": 2,
                  "message": 3
                }
              ],
              "background": {
                "activeOnStart": true,
                // once server is up and running it'll display something like:
                // Fast Debugger (ruby-debug-ide 0.7.2, debase 0.2.4.1, file filtering is supported) listens on 0.0.0.0:1234
                "beginsPattern": "^Fast Debugger.*1234$",
                "endsPattern": ".",
              }
            }
          ],
        // open up a new task window everytime
        // if set to default `shared` then previous task window needs to be closed 
        // otherwise, was having issues getting patternMatcher to work
        // and for rdebug to attach within a shared terminal on successive runs, or restarts. 
        "presentation": {
          "panel": "new"
        }
    }]
}