在 VS Code 上运行 rspec

12
我有类似于这样的rspec测试代码
describe 'Utils' do

  puts 1111
  describe '#time_condition' do
    puts 2221
    it do
      puts 'aaa'
    end
    puts 2223
  end
end

我的 launch.json 配置如下

{
  "name": "RSpec - all",
  "type": "Ruby",
  "request": "launch",
  "cwd": "${workspaceRoot}",
  "program": "${workspaceRoot}/spec/*_rspec.rb",
  "args": [
    "--pattern",
    "*_rspec.rb"
  ]
},

当我在VSCode上运行测试时,出现了以下错误提示:
1111
2221
2223

当我通过命令运行测试时,出现了以下错误:
>rspec spec --pattern *_rspec.rb
1111
2221
2223
aaa
.

Finished in 0.003 seconds (files took 0.23602 seconds to load)
1 example, 0 failures

如您所见,没有"aaa"的输出,意味着"it"没有被执行。

那么......我该如何在VS Code上运行"it"呢?

顺便说一下,我的spec配置文件(通过rspec --init生成)如下:

.rspec

--color
--require spec_helper

spec\spec_helper.rb

RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.profile_examples = 10

  config.order = :random

  Kernel.srand config.seed
end

软件版本:VScode 1.4.0

Ruby扩展:0.5.3

谢谢


我无法在你的rspec文件中看到你的期望结果。 - Bustikiller
@Bustikiller 刚刚删除了。无论是否有期望,'它'都无法运行。 - Clxy
请尝试使用语法it '测试名称' do - Bustikiller
@Bustikiller 不起作用,也没有起作用。正如我所写的,rspec命令成功了。所以我的rspec代码没问题。无论如何还是谢谢。 - Clxy
7个回答

14

最终在Mac上成功地使用VS Code运行RSpec并带有断点!我的创业公司里没有人知道如何做到这一点。请查看Microsoft在GitHub上提供的有关此功能的实用页面:GitHub页面

我的Gemfile。安装3个gems:

source 'https://rubygems.org'

gem 'rspec'
gem 'ruby-debug-ide'
gem 'debase'

我的 launch.json 文件:

{
  "configurations": [
    {
      "name": "Run RSpec - all",
      "type": "Ruby",
      "request": "launch",
      "cwd": "${workspaceRoot}",
      "program": "/Users/[your name]/.rvm/gems/ruby-2.6.3/bin/rspec",
      "args": [
        "--pattern",
        "${workspaceRoot}/spec/**/*_spec.rb"
      ]
    },
    {
      "name": "Debug RSpec - open spec file",
      "type": "Ruby",
      "request": "launch",
      "cwd": "${workspaceRoot}",
      "useBundler": true,
      "pathToBundler": "/Users/[your name]/.rvm/gems/ruby-2.6.3/bin/bundle",
      "pathToRDebugIDE": "/Users/[your name]/.rvm/gems/ruby-2.6.3/bin/rdebug-ide",
      "debuggerPort": "1235",
      "program": "/Users/[your name]/.rvm/gems/ruby-2.6.3/bin/rspec",
      "args": [
        "${file}"
      ]
    },
  ]
}

对于“program”,使用which rspec的值。我的路径假设我是使用 RVM 安装 Ruby 的,而你的可能不同。

对于“pathToBundler”,使用which bundle的值。

对于“pathToRDebugIDE”,使用which rdebug-ide的值。

对于这一行:"${workspaceRoot}/spec/**/*_spec.rb",你的情况可能不同,具体取决于你的测试文件是如何组织的(按文件夹)。

运行 RSpec:

在 VS Code 中,转到顶部菜单中的Terminal -> New Terminal,确保你处于 Ruby 项目的根目录中。

如果需要,点击任意行号左边来设置断点

在 VS Code 左侧的 Bug 图标上点击,从下拉框中选择配置:

  1. Debug RSpec - open spec file 或者
  2. Run RSpec - all

然后,在左上角点击绿色三角形(Start Debugging)。

更简单的方法:安装 VS Code 扩展Rails Test Runner。然后,在你的测试文件上右键单击,以便:

  1. Run all tests in file 或者
  2. Run tests starting from the current line

1
在 Windows 上使用 WSL 也成功了。太棒了! - Jepzen
如果你不想把它放在Gemfile里怎么办?我尝试了gem install ruby-debug-ide,将useBundler设置为false并编辑了路径,但没有成功。 - Jepzen
要为您的Ruby项目安装Gems,您必须将它们放在Gemfile中。如果您不想在生产环境中使用这些Gems,请将它们放在group:test,:development do块中。 - Raymond Gan
这个在2022年仍然有效,但请确保用你机器上安装的Ruby版本替换2.6.4 - Afsan Abdulali Gujarati
当在DevContainer中工作时,请使用"program": "${workspaceRoot}/bin/rspec",并为rspec创建binstub。 - Tom

4

这里是适用于 Linux 的 版本: 2.0.0

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "label": "rspec - all",
      "type": "shell",
      "command": "$(which bundle) exec rspec",
      "group": "test",
    },
    {
      "label": "rspec - one file",
      "type": "shell",
      "command": "$(which bundle) exec rspec ${file}",
      "group": "test",
    },
    {
      "label": "rspec - focus",
      "type": "shell",
      "command": "$(which bundle) exec rspec ${file} --focus",
      "group": "test",
    }

  ]
}

现在运行 Ctrl+Shift+p,并在菜单中选择:任务: ...


谢谢,我会检查一下。我为了配置这个东西挣扎了很久。 - Arup Rakshit

2

好的,我已经解决了! 我的错误是将错误的值设置给程序。 程序必须是rspec路径。

...
{
  "name": "RSpec - all",
  "type": "Ruby",
  "request": "launch",
  "cwd": "${workspaceRoot}",
  "program": "D:/Ruby/Ruby21/bin/rspec",
  "args": [
    "--pattern",
    "${workspaceRoot}/spec/**/*_rspec.rb"
  ]
},
...

你如何调用它? - Arup Rakshit
就像其他人一样,选择“RSpec - all”,然后点击开始调试。顺便说一下,我已经有一段时间没有玩 RSpec 了。 - Clxy

1

我相信这是最简单的配置,适用于所有情况。它运行当前打开的文件,但您可以调整它以运行所有rspec文件。

{
  "name": "RSpec",
  "type": "Ruby",
  "request": "launch",
  "cwd": "${workspaceRoot}",
  "program": "${workspaceRoot}/bin/bundle",
  "args": [
    "exec",
    "rspec",
    "${file}"
  ]
}

1

我发现在调试器配置中使用本地路径不如在launch.json中找到更好的解决方案。

  {
    "version": "0.2.0",
    "configurations": [
        {
            "name": "RSpec - all",
            "type": "Ruby",
            "request": "launch",
            "cwd": "${workspaceRoot}",
            "program": "${workspaceRoot}/bin/rspec",
            "args": [
                "-I",
                "${workspaceRoot}"
            ]
        }
      ]
   }

在这个例子中,你调用了bin文件夹中由变量${workspaceRoot}引用的rspec binstub。现在你也可以将配置原封不动地与其他在项目上工作的人分享。 如果你没有带有binstub的bin文件夹,你可以通过执行以下命令来生成一个:
bundle binstubs rspec-core

请参考:https://bundler.io/man/bundle-binstubs.1.html


0

我开始使用 debug gem。看起来相当简单且未来有保障。

安装宝石和此扩展:https://marketplace.visualstudio.com/items?itemName=KoichiSasada.vscode-rdbg

launch.json 中,我使用当前行的 rspec 调用:

{
  "type": "rdbg",
  "name": "Run rake spec",
  "request": "launch",
  "command": "bundle exec rspec",
  "script": "${file}:${lineNumber}",
  "args": [],
  "askParameters": false
}

我还没有进行过全面测试,但在 Ruby 3.0.2 上尚未遇到任何问题。


0
如果您的项目在其bin文件夹中有rspec,您可以尝试以下操作:
{
  "name": "RSpec - all",
  "type": "Ruby",
  "request": "launch",
  "cwd": "${workspaceRoot}",
  "program": "${workspaceRoot}/bin/rspec",
  "args": [
    "-I",
    "${workspaceRoot}"
  ]
},

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接