Oh inheritacne, wherefore art thou

Is there anyway to achieve inheritance of specifications?

Suppose I had an action like

def index
filter = params[:filter] ? params[:filter] : ‘%’
order = params[:order] ? params[:order] : ‘created_at DESC’
@posts = Post.find(:conditions => “title LIKE #{filter}”, :order =>
order)

end

Now I might have half a dozen specs for this action that test it when
neither :filter nor :order are supplied as params. But now what I
would like to do is test it when one, other or both of the params are
passed to the action. Most of the specs will probably not need to
change, but I would still like to run them in the case where params
are supplied, just to be sure there are no unwanted side effects.
Probably only one or two of the specs need to be modified, for example
to assert that only Posts matching the filter are returned.

It seems to me that a nice way to do this would be if descriptions
could be extended. So I could have a basic description of the action

describe ‘test index w/o params’ do

it ‘should return the right posts’
end

and would then be able to extend it for each case, e.g.

describe ‘test index with filter param’ do;
extends(‘test index w/o params’)

it ‘should return the right posts’ # overrides example in base spec
end

So this tests everything just like the base spec did, but
changes/overrides one of the examples to correctly assert that the
right posts are returned, thus giving me good confidence that the
presence of the param does’nt cause breakage in places I might not
normally bother to test because of my knowledge of the implementation.

I can’t find anything online to this effect. So far the best I have
been able to do is write shared descriptions but this gets messy very
quickly and causes all sorts of problems that I think makes it
undesirable.

Can anyone suggest a nice way for me to achieve the above.

Look up SharedExamplesGroups

http://rspec.info/rdoc/

On Thu, Mar 20, 2008 at 9:03 PM, Pietro M. [email protected]

On Thu, Mar 20, 2008 at 6:03 PM, Pietro M. [email protected]
wrote:

Is there anyway to achieve inheritance of specifications?

Suppose I had an action like

def index
filter = params[:filter] ? params[:filter] : ‘%’
order = params[:order] ? params[:order] : ‘created_at DESC’
@posts = Post.find(:conditions => “title LIKE #{filter}”, :order => order)

end

If you use shared example groups, you can do stuff like

describe FooController do
it_should_behave_like “super filtered out thing”
end

The docs at rspec.info have examples.

As a side note, you can shorten that action (and get rid of the
ternary operators) by doing:
filter = params[:filter] || ‘%’
order = params[:order] || ‘created_at DESC’

Pat

On Mar 20, 2008, at 11:47 PM, Corey H. wrote:

Look up SharedExamplesGroups

http://rspec.info/rdoc/

Yep - and “it_should_behave_like”. SharedExampleGroups subclass
Module, so it fits in with the “inheritance” idea quite nicely.

Scott

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs