[rspec-users] Controller spec: testing that scope is set

Zach Dennis zach.dennis at gmail.com
Mon Apr 20 16:25:38 EDT 2009

On Mon, Apr 20, 2009 at 3:27 PM, Michael Schuerig <michael at schuerig.de> wrote:
> On Monday 20 April 2009, Zach Dennis wrote:
>> On Sun, Apr 19, 2009 at 6:41 PM, Michael Schuerig
> <michael at schuerig.de> wrote:
> [big snip]
>> I think I am starting to understand what you're after. You want to
>> ensure the scope defined in your query_scope configuration block in
>> the controller is used to set the scope on the controller's model.
>> Right?
> Exactly.
>> With the assumption that that is correct, I would probably refactor
>> how your #query_scope method works. Right now you're implicitly going
>> through a QueryScopeBuilder to get a RequestConditioner, in order to
>> access the #find_options and #offset_limit behaviour on that
>> RequestConditioner. I would make your controller deal with one
>> object, perhaps a RequestToQueryTranslator. Your #query_scope method
>> would come out looking like:
>>    def query_scope(options = {}, &config_block)
>>     model_class = extract_resource!(options)
>>     query = RequestToQueryTranslator.translate(controller.request,
>> &config_block)
>>     around_filter(options) do |controller, action|
>>       controller.instance_variable_set(:@offset_limit,
>> query.offset_limit) model_class.send(:with_scope, :find =>
>> query.find_options, &action) end
>>    end
> I don't agress. Both classes have distinct, although related purposes.
> RequestCondition implements the translation from request parameters to
> #find-options. QueryScope(Builder) fits it in with ActionController and
> adds syntactic sugar. The former is easy to spec, for the latter, I've
> been to lazy to figure out a good way.

I'm just suggesting that RequestCondition and QueryScopeBuilder are
encapsulated behind RequestToQueryTranslator, so your controller
doesn't have to know about both of them. Your controller doesn't
actually care about request conditioners and query scope builders. It
only cares that it can translate a request to a query so it can be
used to set the scope on the model.

>> WDYT?
> I've attached the involved files. I try to avoid doing that on mailing
> lists, but we're going in circles otherwise.
> In the meantime I've looked into rspec's message expectation in order to
> add an expectation for a particular scope being set on an ActiveRecord
> class. What I'd need to implement this is a way to get hold of the
> partially mocked class. There may not be one.
> Michael
> --
> Michael Schuerig
> mailto:michael at schuerig.de
> http://www.schuerig.de/michael/
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Zach Dennis

More information about the rspec-users mailing list