トップ «前の日記(2010/11/09 (火) ) 最新 次の日記(2010/11/11 (木) )» 編集 RSS feed

HsbtDiary


2010/11/10 (水) [長年日記]

[Windows] 月刊 Windows Update

今月は削除ツールだけで少なめのアップデートであった。

[rspec][ruby] rspec2 の meta filtering

みんな便利で使っているとは思うけど、今のプロジェクトでこうやって使っていますよという紹介。やりたいことの前提。

  • 画面操作のテストに steak(capybara + rack_test) を使っているが、一部 selenium を使わざるをえない
  • selenium を使う時に Capybara.current_driver を切り替える必要があるけど ExampleGroup ごとに定義するのがだるい
  • rake spec:acceptance で selenium が起動するのが邪魔

というのを rspec2 の filtering を 使って (kakutani が)解決した。

まず、Capybara.current_driver を切り替える時には CapybaraでSeleniumを使うのように before と after で処理が必要なのだけど、これを filtering で何とか仕様とすると以下のようになる。

RSpec.configure do |config|
  config.include Capybara, :type => :acceptance

  config.before(:all, :selenium => true) do
    Capybara.current_driver = :selenium
  end

  config.after(:all, :selenium => true) do
    Capybara.use_default_driver
  end
end

というのを acceptance_helpter.rb に書いた上で ExampleGroup の定義で

describe Foo, :selenium => true do
(snip)
end

とかすると、:selenium => true した ExampleGroup だけが selenium で動くようになる。見やすいし便利。

そして、ここで指定した metadata を RSpec.configuration.exclusion_filter に応用してみる。これは spec_helper.rb に書く。

RSpec.configure do |config|
  # JS=1 if run the selenium
  config.exclusion_filter = {:selenium => true} unless ENV['JS']
end

こうすることで

JS=1 rspec spec

した時は selenium を含んだ spec が全部流れて、

rspec spec

だけだと selenium を除外した spec が流れるようになる。できれば、ENVは使わないで実現したかったんだけど、rspec の configuration_options.rb を見るにそういったものは無いみたい。

他にも metadata や filter をこういうのに使っているよ!というのがあれば asakusa.rb やどこかの日記で教えてください!

本日のツッコミ(全2件) [ツッコミを入れる]
# ursm (2010/11/17 (水) 12:31)

RSpec 2.1.0 で rspec -t selenium, rspec -t ~selenium という指定ができるようになりました。

# hsbt (2010/11/17 (水) 21:47)

便利!!<br>--tags までは調べてたけど、動かなくて右往左往していたのでした。