Capybaraのセレクタで要素を検証する
CapybaraはWebブラウザをシミュレートしてCucumberでのWebアプリケーションの受け入れテストの記述をサポートするものだけど、Viewのspecを書くのにも使える。
group :test do
gem 'capybara'
end
これでbundle install
すれば spec の中で `have_selector'が使えるようになる。
have_selecorの第1引数は css selector を渡すようになっている。
hoge.html.erb_spec.rb
describe 'users/sessions/new.erb.html' do
subject { render }
it 'ログインフォームを表示する' do
subject.should have_selector('input[type=email][name="user[email]"]')
subject.should have_selector('input[type=password][name="user[password]"]')
end
end
css selector のほか、XPathで要素を指定するメソッドも用意されている。
参考: File: README — Documentation for jnicklas/capybara (master)
helperをstubする
fuga.html.erb_spec.rb
describe 'users/sessions/new.erb.html' do
subject { render }
context 'notice メッセージが設定されている場合' do
before do
view.stub(:notice) { "hoge" }
end
it "noticeメッセージが出力される" do
subject.should have_content("hoge")
end
end
end