Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running cucumber tests in vim or macvim

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

like image 994
alexjfno1 Avatar asked May 22 '26 12:05

alexjfno1


1 Answers

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                                                                                           
like image 79
bundacia Avatar answered May 25 '26 08:05

bundacia