cucumber 表のフィーチャを書く

表のフィーチャとかその書き方。cucumberで feature を generate すると、manage_何ちゃら.feature とか出来て、表のスペックをどうこうしている。そのステップ定義はこんな感じのが出来てる。

Then /^I should see the following items:$/ do |items|
  items.raw[1..-1].each_with_index do |row, i|
    row.each_with_index do |cell, j|
      response.should have_selector("table > tr:nth-child(#{i+2}) > td:nth-child(#{j+1})")
 { |td|
        td.inner_text.should == cell
      }
    end
  end
end

items.raw[1..-1] は 1 始まり、タイトル行は読み飛ばす。「th」タグは td:nth-child(#{j+1} で取れ無いからでしょう、呼応して tr:nth-child(#{i+2}) は 「+2」になってる。

have_selectorメソッドはブロックの返り値が真だと OK。うまく行かなくてブロック内コメントアウトして puts だけにしてもやっぱるうまく行かなくてはまった。puts の返り値は nil だった。
ブロック内、正規表現マッチにしたんだけど、手元では td.inner_text.should match(Regexp.new(cell)) ではうまく行かなかった、Regexp.new('\A'+cell+'\z').match(td.inner_text) でなんとか。

      response.should have_selector("table > tr:nth-child(#{i+2}) > td:nth-child(#{j+1})")
 do |td|
        puts "td: #{td.inner_text}, cell: #{cell}"
        #td.inner_text.should match(Regexp.new(cell))
        Regexp.new('\A'+cell+'\z').match(td.inner_text)
      end