No Bugs, No Life

読んだ本や、プログラミング、システム開発等のねたを中心に。文章を書く練習なので少し硬派に書くつもりだけど、どうなることやら。

RSpec環境の設定

MementoWeaver開発記
MWの移行プログラム(以降、mwmig)を作成するにあたってはruby2.0を使用することとしており、そのテスティング環境としてRSpecを使用する。
今回はRSpecでテストを動かすための環境設定の経緯をメモする。

RSpec採用の理由

単純に趣味。
ruby1.9系列からTest::Unitがminitestに置き換えられたので、minitest相当を学ぶんだったらRSpecを触ってみようという程度の理由。
ついでに言うと、今回程度の移行プログラム(ほぼ使い捨て)に対して単体テストを真面目に書くのも正気の沙汰とは思っていない。

RSpecの導入

公式サイト(RSpec.info: home)によると、gemでインストールすればよいらしい。
おもむろにインストールする。

ruby 2.0.0p0 (2013-02-24) [i386-mingw32]

C:\Users\kazyury>gem install rspec
Fetching: rspec-core-2.13.1.gem (100%)
Successfully installed rspec-core-2.13.1
Fetching: diff-lcs-1.2.4.gem (100%)
Successfully installed diff-lcs-1.2.4
Fetching: rspec-expectations-2.13.0.gem (100%)
Successfully installed rspec-expectations-2.13.0
Fetching: rspec-mocks-2.13.1.gem (100%)
Successfully installed rspec-mocks-2.13.1
Fetching: rspec-2.13.0.gem (100%)
Successfully installed rspec-2.13.0
Parsing documentation for rspec-core-2.13.1
Installing ri documentation for rspec-core-2.13.1
Parsing documentation for diff-lcs-1.2.4
Installing ri documentation for diff-lcs-1.2.4
Parsing documentation for rspec-expectations-2.13.0
Installing ri documentation for rspec-expectations-2.13.0
Parsing documentation for rspec-mocks-2.13.1
Installing ri documentation for rspec-mocks-2.13.1
Parsing documentation for rspec-2.13.0
Installing ri documentation for rspec-2.13.0
Done installing documentation for rspec-core, diff-lcs, rspec-expectations, rspec-mocks, rspec (12 sec).
5 gems installed

C:\Users\kazyury>

無事にインストールできたようなので、実行できることを確認する。
とりあえず空ファイルを作成(mwimg_album_spec.rb)し、rspecコマンド(vimから:!rspec %)で実行してみる。

C:\windows\system32\cmd.exe /c (rspec mwmig_album_spec.rb)
No examples found.


Finished in 0.001 seconds
0 examples, 0 failures
Hit any key to close this window...

実行までは出来ているが、折角入れたのだからvim上で実行できるようにしたい。

vim-quickrunでRSpecを動かす設定

Vim-users.jp - Hack #172: RSpecをquickrunする (1/2):に、まさに求めている記事があった。
vim-users.jpには本当にお世話になってます。ありがとうございます。

一部コマンド名だけRSpec1の頃のコマンド名になっているようなので、そこだけ修正。

""""""""""""""""" quickrunの設定
""""""""""""""""" http://vim-users.jp/2010/09/hack172/ のコマンド名だけ修正
let g:quickrun_config = {}
let g:quickrun_config['ruby.rspec'] = {'command': 'rspec'}
augroup RSpec
  autocmd!
  autocmd BufWinEnter,BufNewFile *_spec.rb set filetype=ruby.rspec
augroup END

*_spec.rbというファイル名で中身なしファイルを作成してquickrunでRSpecが動作した。
f:id:kazyury:20130501010420p:plain