rspec - Behaves Like CRUD

You’ve DRYed up your controllers by using decent_exposure.

Now it’s time to DRY up your controller specs as well.

Installation

Add this to your Gemfile:

gem 'rspec-behaves-like-crud'

Add this to your spec_helper:

require 'rspec_behaves_like_crud'

Usage

Then just add the following lines to your controller spec.

let(:valid_attributes)   { FactoryGirl.attributes_for(:campaign) }
let(:invalid_attributes) { { 'url' => nil } }
let(:resource_class)     { Campaign }

include Incrudable

it_behaves_like 'a CRUD controller'

If you want to custom test some actions, you can still save yourself some time by including the general stuff

let(:valid_attributes)   { FactoryGirl.attributes_for(:campaign) }
let(:invalid_attributes) { { 'url' => nil } }
let(:resource_class)     { Campaign }

include Incrudable

describe "GET show" do
  it "responds in a custom way" do
    # Custom tests ...
  end
end

it_behaves_like 'a CRUD index'
it_behaves_like 'a CRUD new'
it_behaves_like 'a CRUD edit'
it_behaves_like 'a CRUD delete'
it_behaves_like 'a CRUD create'
it_behaves_like 'a CRUD update'

# ^^ Note that it_behaves_like 'a CRUD show' is missing

Collaborators

  • Maciej Tomaka