DRYing out controller specs?

I’d like to DRY out my controller specs but am having difficulties doing
so and am hopeful that a more experienced rspec expert can shed some
much-needed light on my woes…

The controllers themselves are using josevalim’s wonderful
inherited_resources gem, so they are incredibly small - I’m basically
looking for something like this on the rspec side…

I’m hoping to find a way to:

  • Define a set of shared examples for each of the 7 common RESTful
    actions (index, show, new, edit, create, update, destroy)

  • include those shared examples in a number of controllers
    (ApplesController, OrangesController)

  • obviously have the shared examples know about the model names,
    something like:

    assigns[@model_name.to_sym].should be_new_record # e.g. GET xxx/new

  • not so obviously have the description of the example reflect the model
    names:

    it “should delete the #{@model_name}” do

  • be very DRY: Ideally, apples_controller_spec and
    oranges_controller_spec would be small: mostly about initialization of
    config (e.g. the model name) and include the various shared specs which
    tested the controller.

I’ve played around with a lot of approaches, but cannot figure out a
clean pattern in which variables such as @model_name are scoped visible
to the ‘it’ descriptions AND the example blocks. I’m probably missing
some wonderful clean ruby-way of doing this.

Thoughts/advice much appreciated!
-Eric

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