Run Rspec on VS Code

Run rspec on VS Code

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

Run Rspec block via hot key in VS Code

You can install this extension https://marketplace.visualstudio.com/items?itemName=noku.rails-run-spec-vscode

And then press Cmd + L to run the specific describe or it

Run bundle exec command Tasks in Visual Studio Code

Thanks to Hurelu in this post, who explains how shell commands are constructed using the definitions in tasks.json, I have been able to define a tasks file that can run both rspec and cucumber, using bundle's exec:

{
"command": "bundle",
"args": ["exec"],
"tasks": [
{
"suppressTaskName": true,
"taskName": "rspec",
"args": [ "rspec", "${file}" ],
"isTestCommand": true
},
{
"suppressTaskName": true,
"taskName": "cucumber",
"args": [ "cucumber", "${file}" ]
}
]
}

RSpec is the test command, so you can use Cmd+Shift+T/Ctrl+Shift+t to run the current file as an RSpec test.

Happy coding!

How to run a single RSpec test?

Not sure how long this has bee available but there is an Rspec configuration for run filtering - so now you can add this to your spec_helper.rb:

RSpec.configure do |config|
config.filter_run_when_matching :focus
end

And then add a focus tag to the it, context or describe to run only that block:

it 'runs a test', :focus do
...test code
end

RSpec documentation:

https://www.rubydoc.info/github/rspec/rspec-core/RSpec/Core/Configuration#filter_run_when_matching-instance_method

How do I run debugger with the Rails run Spec extension?

Add pry-byebug to your gemfile and run bundle install

# Gemfile
gem 'pry-byebug'

Then, whenever you want to inspect a test, add binding.pry inside the test.

# some_spec.rb

it "is not behaving how I want it to" do
binding.pry
expect(my_var).to eq(some_val)
end


Related Topics



Leave a reply



Submit