RspecでControllerのテストをする場合

Rspecでコントローラのテストを書く時にメモ

  1 require 'spec_helper'
  2 
  3 describe BookmarksController do
  4   describe "create_bookmark method" do
  5     context "url, comment, title parameter set " do
  6       before do
  7         @user = FactoryGirl.create(:current_user)
  8         controller.stub(:current_user) {@user}
  9       end
 10       it "response check" do
 11         post "create", bookmark_form: {comment: "test[blog]", title: "test blog" , url: "http://www.yaplog.jp"}
 12         response.code.should eql("302")
 13       end
 14       it "response check" do
 15         post "create", bookmark_form: {comment: "test[blog]", title: "test blog" , url: "http://www.yaplog.jp"}
 16         @user.bookmarks.should_not be_nil
 17         @bookmarks = Bookmark.where('user_id = ?', @user.id)
 18         @bookmarks.size.should be >= 1
 19         @user.bookmarks.size.should eql(@bookmarks.size)
 20       end
 21     end
 22 
 23   end
 24 
 25   describe "new method" do
 26     context "new method url is not nil" do
 27       before do
 28         @user = FactoryGirl.create(:current_user)
 29         controller.stub(:current_user) {@user}
 30       end
 31       it "response code check" do
 32         get "new", page_form: {url: "http://www.yaplog.jp"}
 33         response.code.should eql("200")
 34       end
 35       it "response value check" do
 36         get "new", page_form: {url: "http://www.yaplog.jp"}
 37         assigns[:title].should eql("Yaplog test")
 38       end
 39     end
 40     context "new method url value check" do
 41       before do
 42         @user = FactoryGirl.create(:current_user)
 43         controller.stub(:current_user) {@user}
 44       end
 45       it "url parameter check" do
 46         get "new", page_form: {url: ""}
 47         response.code.should eql("302")
 48       end
 49       it "url parameter check 2" do
 50         get "new", page_form: {hoge: ""}
 51         response.code.should eql("302")
 52       end
 53     end
 54   end
 55 
 56   describe "user_bookmark method" do
 57     context "google users test" do
 58       before do
 59         @google_user = FactoryGirl.create(:test_google_user)
 60         controller.stub(:current_user) {@google_user }
 61       end
 62       it "response code check" do
 63         get "user_bookmarks", email_localpart: 'test_google_user'
 64         response.code.should eql("200")
 65       end
 66       it "bookmark data check" do
 67         get "user_bookmarks", email_localpart: 'test_google_user'
 68         assigns[:users].should_not be_nil
 69         bookmarks = assigns[:users].bookmarks
 70         p bookmarks
 71         bookmarks.size.should eql(1)
 72       end
 73     end
 74   end
 75 
 76 end