No commit activity in last 3 years
No release in over 3 years
A few bits and pieces to make testing controllers easier
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Installation

Add the following to your Gemfile, under the test group

gem 'rspec_controller_helpers', require: false

And add the following in the top of your spec_helper.rb:

require 'rspec_controller_helpers'

Usage

Resourceful Controller

Pre-requisite: You must have a factory set up with the right attributes

This shared example can be used to test the default resourceful rails endpoints.

it_behaves_like 'a resourceful controller', User

By default, it will test all of the following:

Controller Method request Tests
index GET /<resource> The page returns a 200 http status, and it rendering the index template
new GET /<resource>/new The page returns a 200 http status, and it renders the new template
create POST /<resource> The model count changes by 1
show GET /<resource>/:id The page returns a 200 http status, and it renders the show template
edit GET /<resource>/:id/edit The page returns a 200 http status, and it renders the edit template
update PATCH /<resource>/:id All attributes on the object are updated
delete DELETE /<resource>/:id The model count changes by -1

You can disable endpoint tests the same way you would in routes.rb:

# Tests new, create, show, edit, update, and delete
it_behaves_like 'a resourceful controller', User, except: [:index]
# Tests new, create
it_behaves_like 'a resourceful controller', User, only: [:new, :create]

This shared example will also infer your factory name from your model, so if your model is User, it will try create(:user). You can override that with the factory option

it_behaves_like 'a resourceful controller', User, factory: :admin

JSON

By default, the shared example assumes html format for all views, but you can override that for each view, to test json endpoints:

it_behaves_like 'a resourceful controller', User, formats: { index: [:html, :json] }

That will test the following:

Controller Method request Tests
index GET /<resource> The right number of objects is returned
show GET /<resource>/:id The correct attributes are returned

You can also provide the json key value if your data is nested under that controller name:

it_behaves_like 'a resourceful controller', User, formats: { index: [:html, :json] }, json_key: :users

"users":
[
  { ... }
]