stub_chain和should_receive一起使用

Zar*_*doz 13 rspec ruby-on-rails stubbing rspec2

我试图测试在一个方法调用链中,其中一个方法得到一个特定的参数.在下面的代码中,例如MyModel必须接收方法的参数0 offset.不幸的是,下面的代码不起作用.似乎无法混合使用should_receive和stub_chain.我该怎么解决这个问题?我正在使用RSpec 2.

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

我试图测试的代码:

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

更新

我还在RSpec谷歌集团上发布了这个问题,大卫(RSpec的创建者)回答了这个问题(感谢David):http://groups.google.com/group/rspec/browse_thread/thread/6b8394836d2390b0?hl = en

Zar*_*doz 8

这是我现在在我的一个规范中所做的一个例子.这有点不方便(导致许多行),但它有效:

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")
Run Code Online (Sandbox Code Playgroud)

这例如测试SearchPaginationModel.tag_counts.offset(0).limit(X).where(X).order(X)确实offset设置为0.