[rspec-users] Specifying mixins

Tom Stuart tom at experthuman.com
Thu Nov 1 08:33:11 EDT 2007


Hi folks,

Can anyone share some accumulated wisdom about the best way to spec  
mixins in general, and (Jamis Buck-style) ActiveRecord "concerns" in  
particular?

The standard situation here is that there's a bunch of functionality,  
related by concept if not by implementation, that one wants to inherit  
in many different classes (e.g. ActiveRecord models) without having to  
actually use subclassing -- straightforward enough. But since BDD best  
practice encourages one expectation per example and no mocking in the  
behaviour setup, the specification for this shared functionality is  
often spread across many behaviours, each of which may need to do its  
own setup and teardown.

So, how best to mix the mixin spec in with the spec for each class  
that uses the mixin (IYSWIM)? I've tried several permutations of  
helpers, spec mixins, shared-shared behaviours and so on, but can't  
find anything which is persuasively neat and DRY while still working  
reliably. One point of contention is that the mixin's behaviours might  
need to do things like instantiate the target class with specific  
arguments in before :each (or call some other class method, if the  
mixin provides some) so it's not really good enough for the target  
spec to just squirrel away a prebuilt object in an instance variable.

Any advice, please?

Cheers,
-Tom


More information about the rspec-users mailing list