Run rspec on VS Code

后端 未结 4 1105
日久生厌
日久生厌 2021-01-19 14:04

I have rspec test code like this

describe \'Utils\' do

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


        
相关标签:
4条回答
  • 2021-01-19 14:13

    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 讨论(0)
  • 2021-01-19 14:16

    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
    0 讨论(0)
  • 2021-01-19 14:18

    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"
      ]
    },
    ...
    
    0 讨论(0)
  • 2021-01-19 14:22

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

    0 讨论(0)
提交回复
热议问题