Controllerのテストカバレッジ100%化。
(photo credit: vasta via photopin cc)
FactoryGirlも使えるようになったのでいよいよControllerの
テストカバレッジ100%化を仕上げます。
describe '#create' do context 'with validated data' do before do idea = FactoryGirl.build(:idea) post :create, :idea => idea.attributes end it 'should redirect to show page' do response.should redirect_to( :action => :show, :id => Idea.last.id ) end it 'should create a new record' do expect{ post :create, :idea => { :name => 'hage', :description => 'hagehage' } }.to change(Idea, :count).by(1) end end context 'with non-validated data' do before do @idea = Idea.new(:name=>'', :description=>'') end it 'should not be valid' do @idea.should_not be_valid end it 'should render "new" page' do post :create, :idea=> @idea.attributes response.should render_template(:new) end end end
FactoryGirlでcreateしちゃうとpostの処理とか飛ばしていきなりDBにレコード追加しちゃう(?)ので、
buildでオブジェクト作るとこまでやって、データ追加はrspecの方でやる。
ポイントはFactoryGirl.build()を使うのと、つくったオブジェクトをポストするのに
@idea.attributesでパラメータを渡すことですな。
stackoverflow「passing params to post :create request ruby-on-rails-3.1, Rspec, factory-girl」
stackoverflowでようやく解決。はまりました。。
validationでエラーになったとき、てっきりリダイレクトされるのかと思ったらrenderしてるだけやったっていう。
Failure/Error: response.should redirect_to( :action => :show, :id => @id ) Expected response to be a <redirect>, but was <200>
it 'should render "new" page' do post :create, :idea=> @idea.attributes response.should render_template(:new) end
いったんこんな感じかなーと思うものの、まだちょっと気持ちわるいところが残ってる。 全体的にbeforeでどこまで処理するかみたいなのが揃ってないからかな。
パラメータだけ用意してたり、追加処理までしちゃってたり。
このへんは今後もっといいソース見て勉強していきたいところ。
describe '#destroy' do before do 5.times do FactoryGirl.create(:idea) end end context 'when deleting the existing data' do before do delete :destroy, :id => Idea.first.id end it 'should redirect to index' do response.should redirect_to(ideas_path) end it 'should delete a record' do expect{ delete :destroy, :id => Idea.last.id }.to change(Idea, :count).by(-1) end end context 'when deleting unexisting data' do it 'should raise error' do lambda { delete :destroy, :id => Idea.last.id+1 }.should raise_error end end end
こっちは特に問題なくテストできたはず。
ちょっと慣れてきてレコード数減らすのとかもするっとテストできました。
ここまででカバレッジ84.31%を記録。
いい感じやけどベースにしてたブログのテストは全部もう書いちゃったよ。
あと何やればいいんだ。。
describe '#update' do context 'when updating existing data' do before do FactoryGirl.create(:idea) put :update, :id => Idea.first.id, :idea => { :name => "new hogehoge" } end it 'should change the attributes' do Idea.first.name.should eq("new hogehoge") end it 'should redirect to show page' do response.should redirect_to( :action => :show, :id => Idea.first.id ) end end context 'when updating unexisting data' do it 'should raise error' do lambda { put :update, :id => Idea.last.id+1, :idea => { :name=>"new hogehoge" } }.should raise_error end end end
これも特にはまらずテスト完了。これでカバレッジ92.16%。
line missingが3行らしいけどどこだ。。
とここまで来てようやく、Simplecovの画面で詳細表示できることに気づいた!
あとはこのmissing lineをテストすればいいだけじゃないか。
で調べてみたらnewのテストが漏れてたらしい。
ので書いてみる。
describe '#new' do before do get :new end it 'should be success' do response.should be_success end it 'should assign new record' do assigns[:idea] == Idea.new end end
あんま参考がなかったので自信ないけどこれでカバーできました。
最後の1lineがupdateのとこやったんやけど、これはvalidationエラー時のテストがないせいでした。
なのでこれもcontext足してテスト追加する。
describe '#update' do context 'when updating existing data' do before do FactoryGirl.create(:idea) end context 'with validated data' do before put :update, :id => Idea.first.id, :idea => { :name => "new hogehoge" } end it 'should change the attributes' do Idea.first.name.should eq("new hogehoge") end it 'should redirect to show page' do response.should redirect_to( :action => :show, :id => Idea.first.id ) end end context 'with non-validated data' do it 'should render edit page' do put :update, :id => Idea.first.id, :idea => { :name => "" } response.should render_template(:edit) end end end context 'when updating unexisting data' do it 'should raise error' do lambda { put :update, :id => Idea.last.id+1, :idea => { :name=>"new hogehoge" } }.should raise_error end end end
これでついにカバレッジ100%!
大したことしてないのになんか達成感です。
これにてひとまずrspecまわりは終了。
相当勉強になりましたなこれは。。
このアプリを改善って思ってたけど、せっかくなんで
あたらしいアプリ作ってみようかなー。。