コントローラーのindexアクションのテスト
ArticlesController < ActionController::Base def index @articles = Article.all end end
describe ArticlesController do describe "GET 'index'" before { get :index } it { expect(assgins(:articles)).to Article.all } end end end
テストが少ないうちは問題ないんだけど、プロダクションコードでpagerが使われると、@articlesが必ずしもArticle.allメソッドと同じにならなくなっちゃう。
こういう時はallメソッドをstubがいいと思うんだ。
describe ArticlesController do let(:articles) { [FactoryGirl.create(:article)] } describe "GET 'index'" before do Article.stub(:all).and_return(articles) get :index it { expect(assgins(:articles)).to Article.all } end end end
うむ。