- 更新日: 2014年4月22日
 - RSpec
 
RSpec の ControllerSpec でパラメータとともに post 送信するテスト
昨日の Rspec で Controller テストの準備編 | EasyRamble のエントリーに続いて、実際に ControllerSpec でテストをごりごり書いていきます。
ControllreSpec で、コントローラーのアクションにパラメータを渡して post で送信するテストを例示します。通常の POST 送信と Ajax による POST 送信の二種類の方法を書きました。Message モデルに対して Post 送信し、Message モデルのインスタンスを DB に保存する場合のテストの例です。
通常の POST と Ajax の POST で使うメソッド
通常の post。
| 
					 1 2  | 
						post :create, message: hash post :アクション名, パラメータ  | 
					
Ajax での post。
| 
					 1 2  | 
						xhr :post, :create, message: hash xhr :post, :アクション名, パラメータ  | 
					
以上の post 送信用のメソッドを使います。
form_for(@message) のように form_for ヘルパーでフォームを生成した場合、input フィールドの name 属性は “message[title]” となっているはずですので、パラメータは message: hash のように渡します。
SomeController で Message モデルに post 送信する場合のテスト例
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54  | 
						describe SomeController do   let!(:message) { FactoryGirl.build(:message) }   let!(:message_params) { { title: message.title, content: message.content } }   describe "when user is login" do     login_user     describe "about current_user" do       it "should have a current_user" do         expect(subject.current_user).not_to eq nil       end     end     describe "post message" do       it "should increment the Message count" do         expect do           post :create, message: message_params         end.to change(Message, :count).by(1)       end       describe "with Ajax" do         it "should increment the Message count" do           expect do             xhr :post, :create, message: message_params           end.to change(Message, :count).by(1)         end       end     end   end   describe "when user is not login" do     describe "about current_user" do       it "should not have a current_user" do         expect(subject.current_user).to eq nil       end     end     describe "post message" do       it "should not increment the Message count" do         expect do           post :create, message: message_params         end.to change(Message, :count).by(0)       end       describe "with Ajax" do         it "should not increment the Message count" do           expect do             xhr :post, :create, message: message_params           end.to change(Message, :count).by(0)         end       end     end   end end  | 
					
FactoryGirl.build は、FactoryGirl.create と違い、インスタンスを生成するだけでDBに保存しません。この例では、post メソッドともに送信する message_params を組み立てるために使っています。
その後、実際に post 送信を行い、change(Message, :count).by(1) で DB のレコードの増加を確認。ユーザーがログインしている場合は、1つ増えるのを確認しています。delete メソッドの場合は、by(-1) でレコードが1つ減るのを確認できます。
テスト書くの楽しいですね!(実装進めろ俺)
- RSpec の関連記事
 - FactoryGirlをSpringと共に使う時の注意
 - 複数モデルのpost :createをテストするRSpecコード(Controller Spec)
 - RSpec3でTime.nowをスタブ化(stub)
 - RSpecでJSONによるPOSTリクエストをテスト
 - RSpec & Capybara の雑感
 - RSpec+Capybaraで同名の複数要素の並び順をテストする
 - RSpec3ではrails_helper.rbがrequireされる
 - Capybara + Launchy で RSpec テストをブラウザで確認
 - CapybaraのwithinをRSpecで使う
 - Serverspec(RSpec)のテスト出力に色を付けて見やすくフォーマット
 

画像付き英語辞書 Imagict | 英単語をイメージで暗記





Leave Your Message!