Running RSpec Within Vim
Below are a handful of mappings and a function I use to run various incantations of RSpec from within Vim.
noremap <Leader>rs :call RunSpec('spec', '-fp')<CR>
noremap <Leader>rd :call RunSpec(expand('%:h'), '-fd')<CR>
noremap <Leader>rf :call RunSpec(expand('%'), '-fd')<CR>
noremap <Leader>rl :call RunSpec(expand('%'), '-fd -l ' . line('.'))<CR>
function! RunSpec(spec_path, spec_opts)
let speccish = match(@%, '_spec.rb$') != -1
if speccish
exec '!bundle exec rspec ' . a:spec_opts . ' ' . a:spec_path
else
echo '<< WARNING >> RunSpec() can only be called from inside spec files!'
endif
endfunction
When a spec file is open in the current buffer I can do the following:
Mapping | Description |
\rl |
Run the example the includes the current line, e.g., rspec spec/models/foo_spec.rb ~l N. (If the current line is an it block, only that example is run. If the current line is a describe block, all examples within the context are run — I’m particularly fond of this feature of RSpec.) |
\rf |
Run the entire spec file, e.g., rspec spec/models/foo_spec.rb. |
\rd |
Run all the spec files in the current spec file’s directory, e.g., rspec spec/models. |
\rs |
Run the entire spec suite, e.g., rspec spec. |
The way the mappings are configured the first three mappings run rspec with the documentation format. The last one runs rspec with the progress (dots) format. I find the former nice when I’m running a small number of examples and the latter perferable when I’m running a large number of examples.