2013-10-12 18 views
3

我剛剛開始使用VIM作爲我的主編。我到目前爲止發現了很多有趣的小功能。我一直在尋找幾天的時間來從編輯器中運行我的黃瓜測試(如果它可以運行特定的場景,那就是獎金)。我嘗試過像vim-cucumber和vim-vroom這樣的插件,但是我似乎無法讓事情順利進行。注意:我在env.rb中執行「Rake」命令,這可能是爲什麼插件無法正常工作。只是想知道是否有人在同一條船上,並有解決方案,任何幫助,將不勝感激。在vim或macvim中運行黃瓜測試

感謝 亞歷克斯

回答

2

我建立映射,當我鍵入,t我的光標下的RSpec的或黃瓜測試運行,當我輸入,T整個文件,我在運行。如果我不在rspec或黃瓜文件中時鍵入,t,那麼我運行的最後一次測試重新運行。這是我的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                       
1

這可能會讓一些煩人的,但我喜歡重新映射我的測試鍵來運行特定文件的規格。所以我會運行在我的會議

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

這樣,我只能考什麼我的工作,而不是測試所有文件以下。我喜歡這種方法,因爲我可以在我的項目的任何地方編輯任何文件,它仍然只會運行我希望它運行的測試。基本上它說,保存我所在的任何文件並運行規格。如果我運行單元測試我將其更改爲:

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

我喜歡快速的反饋,所以我想只運行涉及到我的情況的測試,而不是整個套件。我將在最後運行整個套件以確保我沒有錯過任何東西。