Rails環境へのTurnip導入で、思わぬハマり方をしたのでメモ。
参考
有名どころですが……。
http://magazine.rubyist.net/?0042-FromCucumberToTurnip#l36
Turnip公式。
https://github.com/jnicklas/turnip#where-to-place-steps
Turnip公式READMEを和訳してくださっている。
http://qiita.com/fakestarbaby/items/2e832be8aa5bc5e93e69
当方の環境
Linux Mint 17
Ruby 1.9.3
Rails 3.2.19
Turnipインストール手順
Gemfileに追記。
group :test do
gem 'capybara'
gem 'capybara-webkit'
gem 'turnip'
end
$ bundle install
いくつかライブラリを追加でインストール要だったが、ここまではクリア。
手始めに.featureと.stepを作成。
spec/features # .feature ファイル群置場
spec/steps # steps.rb ファイル群置場
spec/spec_helper.rbに追記。
Dir.glob("spec/**/*steps.rb") { |f| load f, true }
require 'capybara/dsl'
require 'capybara/rspec'
require 'capybara/webkit'
require 'capybara/poltergeist'
require 'rspec/rails'
require 'rspec/autorun'
require 'turnip'
require 'turnip/capybara'
結果として、↑ この記述は自分の環境ではNGだった。
そして苦難の始まり
で、いざ実行。
$ rspec -r turnip/rspec spec/features/order.feature
spec/steps/order_steps.rb:3:in `<top (required)>': undefined method `step' for main:Object (NoMethodError)
from /home/.../rails_app/bento/spec/spec_helper.rb:20:in `load'
from /home/.../rails_app/bento/spec/spec_helper.rb:20:in `block (2 levels) in <top (required)>'
from /home/.../rails_app/bento/spec/spec_helper.rb:20:in `glob'
from /home/.../rails_app/bento/spec/spec_helper.rb:20:in `block in <top (required)>'
(略)
すごく下らない何かを忘れているようなエラー。
いろいろ試しまくったが、どうも自分の環境では、
Dir.glob("spec/**/*steps.rb") { |f| load f, true }
はspec_helper.rbではなく、
https://github.com/jnicklas/turnip#where-to-place-steps
公式の
Turnip also tries to load a file called turnip_helper where you can setup anything specific to your turnip examples. You might find it beneficial to load your steps from this file so that they don't have to be loaded when you run your other tests.
この提言に従い、ternip_helper.rbに書かねばダメらしい。
出直し
require 'rails_helper'
Dir.glob("spec/steps/**/*steps.rb") { |f| load f, true }
すると、
$ rspec spec/features/order.feature
/var/lib/gems/1.9.1/gems/turnip-1.2.3/lib/turnip/step_definition.rb:9: warning: assigned but unused variable - trace
No examples found.
Finished in 0.0003 seconds (files took 0.23294 seconds to load)
0 examples, 0 failures
「undefined method `step' for main:Object (NoMethodError)」は消えたが、サンプルfeatureを読み込めない。
あっけない幕切れ
もうstackoverflow.comで聞こうか、でもfeatureとstepが日本語のままじゃなんだし、とサンプルfeatureを読み返し、
# encoding: utf-8
# language: ja
機能: ポータル画面からログイン
シナリオ: トップページにアクセスしてログインする
前提 hoge サイトにアクセスする
もしトップページを表示する
ならば 画面にようこそと表示されていること
あれ? よくよく見ると、インデントが全角スペースになっているが、大丈夫なのか? と、インデントを半角スペース2バイトずつに直して再実行すると、
Finished in 0.23904 seconds (files took 7.29 seconds to load)
1 example, 0 failures
ようやく動いた。
featureを少し書き換え、テストが失敗もすることを確認。
教訓
わかっちゃいるんですけど。
- 参考サイトからのコピペには注意しましょう。
- 公式Readmeはちゃんと読みましょう。
ほか懸念事項
Rspec実行時に、
/var/lib/gems/1.9.1/gems/turnip-1.2.3/lib/turnip/step_definition.rb:9: warning: assigned but unused variable - trace
/var/lib/gems/1.9.1/gems/mysql2-0.3.16/lib/mysql2/error.rb:22: warning: method redefined; discarding old sql_state=
/var/lib/gems/1.9.1/gems/sass-rails-3.2.6/lib/sass/rails/railtie.rb:36: warning: possibly useless use of :: in void context
やら何やら、やたらとワーニングが出ているのは、いつかなんとかしないといけないのかも知れない。