stub_chain together with should_receive

浪子不回头ぞ 提交于 2019-12-03 16:31:41

问题


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")

Update

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


回答1:


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.



来源:https://stackoverflow.com/questions/4262181/stub-chain-together-with-should-receive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!