Question

I have just recently started using VIM as my main editor. Im having great fun so far discovering all the nice little features that it offers. I have been looking for a couple of days now for someway to run my cucumber tests from within the editor (a bonus if it can run specific scenarios). I have tried plugins like vim-cucumber and vim-vroom but I can't seem to get things working. Note: I do things in env.rb like running the "Rake" command which maybe why the plugins aren't working. Just wondering if anybody has been in the same boat and has a solution and any help would be appreciated greatly.

Thanks Alex

Was it helpful?

Solution

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                                                                                           

OTHER TIPS

This may get annoying to some, but I like to remap my test keys to run specs for a particular file. so i'll run the following in my session

:map ,t :w\|:!cucumber features/my_cucumber_feature.feature<cr>

That way I can test only what i'm working on and not have to test all files. I like this method because I can edit any file anywhere in my project and it will still only run the test I want it to run. Basically it says; save whatever file i'm on and run the specs. If i'm running unit tests I change it to:

:map ,t :w\|:!rspec spec/models/my_model_spec.rb<cr>

I like quick feedback so I like to run only the tests that relate to my situation and not the whole suite. I will run the whole suite at the end to make sure I didn't miss anything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top