2010-11-23 9 views
13

メソッドを呼び出すメソッドでメソッドの1つが特定のパラメータを取得するかどうかをテストしようとしています。以下のコードでは、MyModelはメソッドoffsetのパラメータ0を受け取る必要があります。残念ながら、以下のコードは動作しません。 should_receiveとstub_chainを混在させることはできないようです。どうすればこの問題を解決できますか?私もRSpecのGoogleグループに質問を投稿 stub_chain with should_receive

デビッド(RSpecののクリエイターを

tags = taggable.tag_counts.offset(page-1).limit(per_page).where(*where_clause).order("count DESC") 

更新された:私はRSpecの2

MyModel.should_receive(:offset).with(0).stub_chain(:tag_counts, :offset, :limit, :order).and_return([]) # does not work! 

コード私がテストしようとしているを使用しています)回答しました(デイビッドに感謝します):http://groups.google.com/group/rspec/browse_thread/thread/6b8394836d2390b0?hl=en

答えて

8

これは私が現在私の仕様の1つで行っている例です。それは少し不便(多くの行の原因)ですが、それは動作します:本当にoffsetは0が設定されているSearchPaginationModel.tag_counts.offset(0).limit(X).where(X).order(X)

SearchPaginationModel.stub(:tag_counts) { SearchPaginationModel } 
SearchPaginationModel.should_receive(:offset).with(0) { SearchPaginationModel } 
SearchPaginationModel.stub_chain(:limit, :where, :order) { [] } 
SearchPaginationModel.stub_chain(:tag_counts, :where, :count).and_return(1) 
SearchPaginationModel.search_tags(:page => "1") 

この例のテスト。

関連する問題