[Ruby on Rails 3, Ruby on Rails 4] RSpec/Capybara: Gurad でテストを自動化する

つづき。
「Ruby on Rails チュートリアル」をさわってみます。

[markdown]

> * [RSpec/Capybara: rspec テストを試してみる | deadwood](https://www.d-wood.com/blog/2013/10/11_4816.html)
> * [第3章 ほぼ静的なページの作成](http://railstutorial-ja.herokuapp.com/chapters/static-pages#top)

Guardはファイルシステムの変更を監視し、自動的にテストを実行する。
RSpec 以外にも、Test::Unit、Cucumber、minitestなど多くの環境向けの機能拡張がある。

> * [ASCIIcasts – “Episode 264 – Guard”](http://ja.asciicasts.com/episodes/264-guard)

## Gemfile

“`ruby
group :development, :test do
gem ‘sqlite3’, ‘1.3.5’
gem ‘rspec-rails’, ‘2.11.0’
gem ‘guard-rspec’
end
group :test do
gem ‘capybara’, ‘1.1.2’
gem ‘rb-fsevent’, :require => false
# gem ‘growl’
gem ‘terminal-notifier-guard’
end
“`

### rb-fsevent, growl, terminal-notifier-guard

Mac OS X 環境下の Guard 利用の前提条件としてインストールが必要。
Guard は、gem growl をインストールすることで、Growlの通知機能をがサポートされるが必須ではない。

> * [bundle exec を省略してGrowl対応しておく | deadwood](https://www.d-wood.com/blog/2013/10/10_4806.html)

下記のgemで、OS の通知センターを利用できるよう。

“`
gem ‘terminal-notifier-guard’
“`

Growlは、後述のエラーでうまく動かなかったので、こちらを利用した。

> * [RSpec/Spork/Guard/Growl/Rails 3.2.11で作る – プリチーなTDD環境! – 酒と泪とRubyとRailsと](http://morizyun.github.io/blog/guard-spork-rspec-tdd/)

Mac 以外の開発環境は、以下のgemを利用する。

“`ruby
# Test gems on Linux
group :test do
gem ‘capybara’, ‘1.1.2’
gem ‘rb-inotify’, ‘0.8.8’
gem ‘libnotify’, ‘0.5.9’
end
# Test gems on Windows
group :test do
gem ‘capybara’, ‘1.1.2’
gem ‘rb-fchange’, ‘0.0.5’
gem ‘rb-notifu’, ‘0.0.4’
gem ‘win32console’, ‘1.3.0’
end
“`

## インストールと初期化

“`
% bundle install
% bundle exec guard init rspec
19:33:12 – INFO – Writing new Guardfile to /Users/****/projects/sample_app/Guardfile
19:33:12 – INFO – rspec guard added to Guardfile, feel free to edit it
“`

## Guard を起動する

“`
% bundle exec guard
20:31:35 – INFO – DEPRECATION WARNING: The :version option is deprecated. Only RSpec 2 is now supported.
20:31:35 – INFO – Guard is using TerminalNotifier to send notifications.
20:31:35 – INFO – Guard is using TerminalTitle to send notifications.
20:31:35 – INFO – Guard::RSpec is running
20:31:35 – INFO – Running all specs
……..
Finished in 0.34951 seconds
8 examples, 0 failures
Randomized with seed 60671
20:31:41 – INFO – Guard is now watching at ‘/Users/****
/projects/sample_app’
[1] guard(main)>
“`

rspecが実行され、結果が通知センター機能で通知される。

Gurad 2013-10-10 20.26.54

そのままコンソールが表示される。
この状態で監視されているファイルを変更すると、rspecが走る。

たとえば、config/routes.rb を空にする。

“`
20:42:20 – INFO – Running: spec/routing
/Users/****
/projects/sample_app/vendor/bundle/ruby/2.0.0/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load’: cannot load such file — /Users/****
/projects/sample_app/spec/routing (LoadError)
from /Users/****
/projects/sample_app/vendor/bundle/ruby/2.0.0/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `block in load_spec_files’
from /Users/****
/projects/sample_app/vendor/bundle/ruby/2.0.0/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `map’
from /Users/****
/projects/sample_app/vendor/bundle/ruby/2.0.0/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files’
from /Users/****
/projects/sample_app/vendor/bundle/ruby/2.0.0/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in `run’
from /Users/****
/projects/sample_app/vendor/bundle/ruby/2.0.0/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in `run’
from /Users/****
/projects/sample_app/vendor/bundle/ruby/2.0.0/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `block in autorun’
[1] guard(main)>
“`

Gurad 2013-10-10 20.27.46

ファイルを修正して、コンソールを空リターンしてみた。

“`
[1] guard(main)>
[1] guard(main)>
20:46:33 – INFO – Run all all
20:46:33 – INFO – Running all specs
……..
Finished in 0.28373 seconds
8 examples, 0 failures
Randomized with seed 16876
[2] guard(main)>
“`

quit で終了する。

## Guardfile で設定を変更する

Guardfile を修正することで、振る舞いを変更することができます。

### 余分なテストの実行を抑える

失敗したテストが後にパスしたとき、他の余分なテストが実行されないようにする。

“`
guard ‘rspec’, :version => 2, :all_after_pass => false do
“`

> * [3.6.2Guardによるテストの自動化](http://railstutorial-ja.herokuapp.com/chapters/static-pages#sec-guard)

### watch コマンドを追加する

下記とのことだが、Guardfile をみると .erb や .haml もチェックされるようなので後で試す。

> デフォルトのGuardfileでは一致するすべてのファイルはRubyファイルです。ビューファイルが変更されたときもspecを実行したいので、これに一致するwatchコマンドを新たに追加します。

“`
watch(%r{^app/views/(.+)/}) { |m| “spec/requests/#{m[1]}_spec.rb” }
“`

> * [ASCIIcasts – “Episode 264 – Guard”](http://ja.asciicasts.com/episodes/264-guard)

## 補遺

Gurad 2013-10-10 19.43.30

Growl通知がでるものの、Guard が強制終了するようなので調べるも、解決できなかったので、Growlをやめました。

### Bad file descriptor (Errno::EBADF)

下記のような指定をすると

“`
gem ‘rb-fsevent’, ‘0.9.1’, :require => false
gem ‘growl’, ‘1.0.3’
“`

このようなエラーが出たので、バージョン指定を外し、bundle update しました。

“`
19:55:04 – INFO – Guard is now watching at ‘/Users/****/projects/sample_app’
/Users/****/projects/sample_app/vendor/bundle/ruby/2.0.0/gems/rb-fsevent-0.9.1/lib/rb-fsevent/fsevent.rb:40:in `select’: Bad file descriptor (Errno::EBADF)
from /Users/****/projects/sample_app/vendor/bundle/ruby/2.0.0/gems/rb-fsevent-0.9.1/lib/rb-fsevent/fsevent.rb:40:in `run’
from /Users/****/projects/sample_app/vendor/bundle/ruby/2.0.0/gems/listen-0.7.3/lib/listen/adapters/darwin.rb:31:in `block in start’
“`

### Error sending notification with growl

Guard は機能するようになったものの、下記のGrowlエラーが解決できませんでした。

“`
20:23:33 – ERROR – Error sending notification with growl: undefined method `silent=’ for #
“`

[/markdown]