I am trying to test if in a method calling chain one of the methods get a specific parameter. In the below code for example MyModel must receive the parameter 0 for the method offset
. Unfortunately the code below does not work. It seems it is not possible to mix should_receive and stub_chain. How could I solve this? I am using RSpec 2.
MyModel.should_receive(:offset).with(0).stub_chain(:tag_counts, :offset, :limit, :order).and_return([]) # does not work!
The code I am trying to test:
tags = taggable.tag_counts.offset(page-1).limit(per_page).where(*where_clause).order("count DESC")
I also posted the question on the RSpec Google Group were David (the creator of RSpec) answered it (thanks David): http://groups.google.com/group/rspec/browse_thread/thread/6b8394836d2390b0?hl=en
This is an example what I do in one of my specs now. It's a bit unhandy (cause of the many lines), but it works:
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")
This for example tests in SearchPaginationModel.tag_counts.offset(0).limit(X).where(X).order(X)
that really offset
0 is set.