gem install rake serverspec --no-ri --no-rdoc
# rake と serverspec gemをインストールwhich serverspec-init
# ひな形生成コマンドが入っていることを確認
( sudoでインストールするのが嫌な方、ruby 1.8.7 は論外という方はrbenvを使おう )
mkdir local-directories-spec
cd local-directories-spec
serverspec-init
- OS typeはUN*Xを選択
- backendはExec(local)を選択
cd local-directories-spec
rake -T
# rake spec:localhost というタスクが出来ていることを確認sed -i "1i require \"rubygems\"" spec/spec_helper.rb
# 1.8.7 はgemを自動で呼んでくれないので・・・cat spec/localhost/sample_spec.rb
# 実際のテストコードの確認rake spec:localhost
# テストの実行- 4件エラーが出る。テンプレートだとApacheのインストールをテストしているが、今回はファイル構成テストを行う。
mkdir -p ~/test/testdir
# テスト対象のディレクトリを作成rm spec/localhost/sample_spec.rb
# サンプルは消すvim spec/localhost/directories_spec.rb
# テストコードの作成
require 'spec_helper'
describe file("#{ENV['HOME']}/test/testdir") do
it { should be_directory }
it { should be_writable }
end
rake spec:localhost
# テストの実行
cat > ~/test/testdir/testfile
# 以下の内容を書き込む
hello
vim spec/localhost/directories_spec.rb
# テストケースの追加
require 'spec_helper'
describe file("#{ENV['HOME']}/test/testdir") do
it { should be_directory }
it { should be_writable }
end
describe file("#{ENV['HOME']}/test/testdir/testfile") do
it { should be_file }
it { should be_readable }
its(:content) { should match /hello/ }
end
rake spec:localhost
# テストの実行