Want to see the full-length video right now for free?
Hi! Laila Winner from thoughtbot here. In this Vim screencast, I'll show you how to use the vim-rspec plugin to make running tests a more efficient, enjoyable and Vim-tastic experience.
vim-rspec is a lightweight plugin that does exactly what you would expect: it runs Rspec tests from Vim. Like guard and autotest, vim-rspec is designed to tighten the feedback loop in the red-green-refactor cycle of test-driven development.
The first step is to install vim-rspec. Because I'm using vundle to manage my Vim plugins, I'll install vim-rspec by adding it to my list of bundles, and running the BundleInstall command:
** .vimrc.bundles
bundle 'thoughtbot/vim-rspec'
[~] vim +BundleInstall +qall
I run the BundleList command to make sure installation was successful:
[~] vim +BundleList
Next, I create a file for my vim-rspec configuration:
[~] touch .vim/rspec
I open my .vimrc and add a command to pull in the contents of my .vim/rspec
configuration file:
source ~/.vim/rspec
Now that vim-rspec is set up, I'm going to add some commands to my configuration file. vim-rspec exposes several functions for running tests individually and in groups. By binding the functions to key mappings, I can run tests by simply pressing a few keys.
First, I customize the Rspec command to runs specs using bundle exec:
let g:rspec_command = '!bundle exec spec {spec}'
Then I bind the vim-rspec function RunCurrentSpecFile() to the key mapping "leader t":
nnoremap <Leader>t :call RunCurrentSpecFile()<CR>
We can see the effects of this change immediately. When I open up a spec file and press space + 't', Rspec runs the spec file.
Going back to my .vim-rspec
file, I bind two additional vim-rspec functions to the key mappings leader s
and leader l
:
nnoremap <Leader>s :call RunNearestSpec()<CR>
nnoremap <Leader>l :call RunLastSpec()<CR>
If I go back to my spec file, we'll see that typing space + s
runs the nearest spec. Typing space L
runs the most recently run spec.
The last thing I want to cover is customizing vim-rspec for use with tools that preload the Rails environment, such as zeus and spring. To speed tests up, I'm going to use the spring gem and configure vim-rspec to use spring.
Because spring is already installed on my system, all I have to do is change the Rspec command in my .vim/rspec
configuration:
let g:rspec_command = '!time spring spec {spec}'
I add the time command so we see how quickly tests are being run.