I set up mappings to that when I type ,t
the rspec or cucumber test under my cursor is run, and when I type ,T
the whole file I'm in is run. If I type ,t
when I'm not in a rspec or cucumber file then the last test I ran get's re-run. Here's the relevant part of my vimrc.
" Set the leader
let mapleader = ","
" Run tests
autocmd FileType ruby,eruby,yaml,haml,scss,cucumber nmap <leader>t :call RunTestCommand(line('.'))<CR>
autocmd FileType ruby,eruby,yaml,haml,scss,cucumber nmap <leader>T :call RunTestCommand()<CR>
function! GetTestCommand()
if expand('%:r') =~ '_spec$'
return 'bundle exec rspec'
elseif expand('%') =~ '\.feature$'
return 'bundle exec cucumber'
else
return '0'
endif
endfunction
function! RunTestCommand(...)
let cmd = GetTestCommand()
" if there's a command update the test command register (t)
if cmd != '0'
let @t = ':!' . cmd . ' ' . expand('%') . (a:0 == 1 ? ':'.line('.') : '')
endif
" if the test command register isn't empty, excecute it.
if strlen(@t) > 0
execute @t
elseif
echoerr "No test command to run"
end
endfunction