【问题标题】:Run rspec on VS Code在 VS Code 上运行 rspec
【发布时间】:2017-01-10 02:40:57
【问题描述】:

我有这样的 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”输出,意味着没有“它”被执行。 那么...我怎样才能让它在 vscode 上运行?

顺便说一下,我的规范配置文件(由 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 文件中看不到你的期望
  • @Bustkiller 刚刚删除。不管有没有期望,'it' 都无法运行。
  • 尝试改用语法it 'test name' do
  • @Bustkiller 不会也没有工作。正如我所写, rspec 命令成功。所以我的 rspec 代码很好。还是谢谢

标签: ruby visual-studio-code


【解决方案1】:

好的。我解决了! 我的错是为程序设置了错误的值。 程序必须是 rspec 路径。

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

【讨论】:

  • 你如何调用它?
  • 和其他的一样,选择“RSpec - all”,然后点击开始调试。顺便说一句,我很久没玩 RSpec了。
【解决方案2】:

这里是version: 2.0.0 用于 Linux

{
  // 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 并在菜单中:Tasks: ...

【讨论】:

  • 谢谢,我会检查一下。我很难为我配置这个。
【解决方案3】:

如果您的项目在其bin 文件夹中有rspec,您可以尝试以下操作:

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

【讨论】:

    【解决方案4】:

    终于让 RSpec 在 Mac 上运行在带有 断点 的 VS Code 上!我的创业公司没有人知道怎么做。请参阅微软方便的GitHub page on it

    我的Gemfile。安装 3 个 gem:

    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}"
          ]
        },
      ]
    }
    

    对于“程序”,使用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

    【讨论】:

    • 在 Windows 上也可以使用 WSL。不错!
    • 如果您不希望它出现在您的 gemfile 中怎么办?我尝试做 gem install ruby​​-debug-ide ,将 useBundler 设置为 false 并编辑了路径,但它没有工作
    • 要安装 Gems,对于您的 Ruby 项目,您必须将它们放在您的 Gemfile 中。如果您不希望在生产中使用这些 Gem,请将它们放在 group :test, :development do 块中。
    【解决方案5】:

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

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

    在本例中,您调用${workspaceRoot} 变量引用的bin 文件夹中的rspec binstub。现在,您还可以与从事该项目的其他人按原样共享配置。 如果您没有带有 binstub 的 bin 文件夹,您可以通过执行以下命令生成一个:

    bundle binstubs rspec-core
    

    见:https://bundler.io/man/bundle-binstubs.1.html

    【讨论】:

      【解决方案6】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-10
        • 2020-04-27
        • 1970-01-01
        • 2017-10-20
        • 2016-08-14
        • 2019-09-14
        • 2021-02-10
        • 2023-04-08
        相关资源
        最近更新 更多