11

I have rspec test code like this

describe 'Utils' do

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

my launch.json like this

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

when I run test on vscode, I got

1111
2221
2223

when I run test by command, got

>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

As your can see, no 'aaa' output, means no 'it' was executed. So...How can I make 'it' to run on vscode?

by the way, my spec config files (generated by rspec --init)like

.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 Extensions : 0.5.3

thanks

Gama11
  • 31,714
  • 9
  • 78
  • 100
Clxy
  • 505
  • 1
  • 5
  • 13

7 Answers7

14

Finally got RSpec to run on VS Code, with breakpoints, on a Mac! No one at my startup knew how. See Microsoft's handy GitHub page on it.

My Gemfile. Install 3 gems:

source 'https://rubygems.org'

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

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

For "program", use value of which rspec. My path assumes I installed Ruby with RVM. Yours will be different.

For "pathToBundler", use value of which bundle

For "pathToRDebugIDE", use value of which rdebug-ide

For this line: "${workspaceRoot}/spec/**/*_spec.rb", yours may differ, depending how your spec files are organized, by folder.

To run RSpec:

In VS Code, go to Terminal -> New Terminal in top menu. Be sure you're in root directory of your Ruby project.

Click left of any line number to set breakpoints, if you like.

Click Bug icon on left side of VS Code. Pick configuration from dropdown box in upper left:

  1. Debug RSpec - open spec file or
  2. Run RSpec - all

Then click green triangle (Start Debugging) on upper left.

Even easier: install VS Code Extension Rails Test Runner. Then right-click on your spec file to either:

  1. Run all tests in file or
  2. Run tests starting from the current line
Raymond Gan
  • 4,612
  • 3
  • 24
  • 19
  • 1
    Got this working with WSL on Windows as well. Nice! – Jepzen May 18 '21 at 13:43
  • What if you dont want it in your gemfile? I tried doing gem install ruby-debug-ide , set useBundler to false and editied the path but it didnt work – Jepzen Jun 07 '21 at 06:04
  • To install Gems, for your Ruby project, you must put them in your Gemfile. If you don't want these Gems in production, put them in a `group :test, :development do` block. – Raymond Gan Jun 07 '21 at 16:59
  • This still works in 2022, however, make sure to replace `2.6.4` with the version of ruby installed on your machine. – Afsan Abdulali Gujarati Apr 21 '22 at 20:31
  • When working in a DevContainer use `"program": "${workspaceRoot}/bin/rspec"`, and create a binstub for rspec. – Tom May 15 '23 at 08:49
4

Here is version: 2.0.0 for 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",
    }

  ]
}

Now run Ctrl+Shift+p and in menu: Tasks: ...

Roman Kiselenko
  • 43,210
  • 9
  • 91
  • 103
2

OK. I solved it! My fault is setting wrong value to program. Program must be rspec path.

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

I found a better solution than using local path in the debugger configuration. in launch.json

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

In this example you call the rspec binstub in the bin folder referenced by the ${workspaceRoot} variable. Now you can also share the configuration as-is with other people working on the project. If you don't have the bin folder with the binstub you can generate one by executing:

bundle binstubs rspec-core

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

Berzelius
  • 11
  • 3
1

This I believe is the simplest configuration that should work for every case. It runs the current open file but you can tweak it so it runs all rspec files.

{
  "name": "RSpec",
  "type": "Ruby",
  "request": "launch",
  "cwd": "${workspaceRoot}",
  "program": "${workspaceRoot}/bin/bundle",
  "args": [
    "exec",
    "rspec",
    "${file}"
  ]
}
Marcos
  • 1,043
  • 10
  • 15
0

If your project has rspec in its bin folder, you can try the following:

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

I switched to using the debug gem. Seems to be quite straightforward and future proof.

Install the gem and this extension: https://marketplace.visualstudio.com/items?itemName=KoichiSasada.vscode-rdbg

In launch.json I use a current line rspec call:

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

I haven't tested this extensively but haven't had any issues yet on Ruby 3.0.2

Sebastian
  • 2,154
  • 1
  • 26
  • 42