rspec-rails で良く使う割に覚えづらいCSSセレクタと、without_tag の話


忘れるのでメモ

nth-of-type(n)


n番目の要素にヒット
ul, li とか、順番の確認が必要なヘルパのチェックに使う

:not([属性])


指定した属性を持たない要素にヒット
option タグの selected を持たない要素の確認に使う

  subject.should have_tag('select') do
    with_tag('option', 4)
    with_tag('option[value=]:not([selected]):nth-of-type(1)', '選択してください')
    with_tag('option[value=10]:not([selected]):nth-of-type(2)', '選択肢A')
    with_tag('option[value=30][selected=selected]:nth-of-type(3)', '選択肢B')
    with_tag('option[value=40]:not([selected]):nth-of-type(4)', '選択肢C')
  end

without_tag


have_tag内で、指定した要素を持たない事を確認。with_tagの逆

  it 'divタグの中に、img タグが無いこと' do
    subject.have_tag('div[class=test]') do
      without_tag('img')
    end


後は、このチートシートがあれば大丈夫
http://cheat.errtheblog.com/s/have_tag/