Rails 3, RSpec 2.5: Using should_receive or stub_chain with named scopes

前端 未结 2 1160
情话喂你
情话喂你 2021-02-02 01:03

I use Rails 3.0.4 and RSpec 2.5. In my controllers I use named scopes heavily, for example

   @collection = GuestbookEntry.nonreplies.bydate.inclusive.paginate(
              


        
相关标签:
2条回答
  • 2021-02-02 01:22

    I would wrap such a complicated query in its own scope, and stub that.

    0 讨论(0)
  • 2021-02-02 01:32

    This problem has bugged me for a while too!

    I believe the reason the behaviour is different from Rails 2 is because the query is no longer being performed during the variable assignment in the controller. Instead, it's lazy-loaded as required.

    I agree with Mark Wilden that it's better to wrap all of these scopes in a larger scope and spec that in your model. This scope clearly has a specific function, so just as one would spec the behaviour of a method which calls several other methods, you would spec the behaviour of a scope that joins several other scopes.

    0 讨论(0)
提交回复
热议问题