微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – stub_chain和should_receive

我试图测试一个方法调用链中的一个方法获取一个特定的参数.在下面的代码中,例如MyModel必须接收方法offset的参数0.不幸的是,下面的代码不起作用.似乎不可能混合使用should_receive和stub_chain.我该如何解决?我正在使用RSpec 2.
MyModel.should_receive(:offset).with(0).stub_chain(:tag_counts,:offset,:limit,:order).and_return([]) # does not work!

我试图测试的代码

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

更新

我还发布了RSPec Google Group的问题,David(RSpec的创始人)回答了(感谢David):http://groups.google.com/group/rspec/browse_thread/thread/6b8394836d2390b0?hl=en

解决方法

这是我现在所做的一个规范中的一个例子.这有点不方便(原因很多行),但它的工作原理:
SearchPaginationModel.stub(:tag_counts) { SearchPaginationModel }
SearchPaginationModel.should_receive(:offset).with(0) { SearchPaginationModel }
SearchPaginationModel.stub_chain(:limit,:where,:order) { [] }
SearchPaginationModel.stub_chain(:tag_counts,:count).and_return(1)
SearchPaginationModel.search_tags(:page => "1")

这样就可以设置真正偏移0的SearchPaginationModel.tag_counts.offset(0).limit(X).where(X).order(X)中的测试.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐