[rspec-users] DRY specs for RESTful controllers

Tom Stuart tom at experthuman.com
Wed Aug 29 07:06:12 EDT 2007


I'm developing a RESTful Rails application whose code and specs are  
becoming increasingly repetitive: every controller does pretty much  
the same set of things (give or take the occasional twiddle), for  
example, and so every controller spec is almost identical modulo some  
names, constants and strings.

The make_resourceful plugin, which autogenerates the boilerplate REST  
actions, has gone a long way towards DRYing out the controller side  
of things, but I haven't seen any examples of nice patterns for doing  
something similar on the spec side.

I'm starting small here: I don't even know what the authorised idiom  
is for sharing behaviours between specs, since I've only used shared  
behaviours within a single spec. (Stick something -- a behaviour?  
some kind of helper? -- in a module? What to call the module file?;  
where to put it?) Furthermore it's obvious that I don't want just to  
"inherit" a single static spec across multiple controllers, but  
rather to instantiate a parameterised spec so that it talks about the  
right classes, the right routes, the right instance variable.  
*Furthermore* furthermore, controllers occasionally deviate from the  
standard RESTful template in a variety of ways, so it'd be nice to be  
able to "override" the appropriate bits after declaring that  
it_should_behave_like "ARestfulController" (or whatever).

I fear the solution to my requirements is ultimately on par with  
implementing some kind of make_resourceful-for-specs plugin, which is  
way beyond what I've got time to do at the moment, but I still feel  
there's plenty of room for gentle incremental improvement on my  
current position without going crazy with speculative generality. Any  
advice much appreciated!


More information about the rspec-users mailing list