Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dkniffin/rspec_controller_helpers
A few bits and pieces to make testing controllers easier
https://github.com/dkniffin/rspec_controller_helpers
rails rspec
Last synced: 4 months ago
JSON representation
A few bits and pieces to make testing controllers easier
- Host: GitHub
- URL: https://github.com/dkniffin/rspec_controller_helpers
- Owner: dkniffin
- License: mit
- Created: 2016-02-18T20:00:40.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-03-14T14:38:26.000Z (almost 9 years ago)
- Last Synced: 2024-03-25T17:28:29.318Z (10 months ago)
- Topics: rails, rspec
- Language: Ruby
- Size: 11.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![](https://badge.fury.io/rb/rspec_controller_helpers.svg)
![](http://ruby-gem-downloads-badge.herokuapp.com/rspec_controller_helpers)
![](https://img.shields.io/badge/license-MIT-blue.svg)# Installation
Add the following to your Gemfile, under the `test` group
````ruby
gem 'rspec_controller_helpers', require: false
````And add the following in the top of your `spec_helper.rb`:
````ruby
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.
````ruby
it_behaves_like 'a resourceful controller', User
````By default, it will test all of the following:
| Controller Method | request | Tests |
|-------------------|-----------|-------|
| `index` | `GET /` | The page returns a 200 http status, and it rendering the index template |
| `new` | `GET //new` | The page returns a 200 http status, and it renders the new template |
| `create` | `POST /` | The model count changes by 1 |
| `show` | `GET //:id` | The page returns a 200 http status, and it renders the show template |
| `edit` | `GET //:id/edit` | The page returns a 200 http status, and it renders the edit template |
| `update` | `PATCH //:id` | All attributes on the object are updated |
| `delete` | `DELETE //:id` | The model count changes by -1 |You can disable endpoint tests the same way you would in `routes.rb`:
````ruby
# 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
````ruby
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:
````ruby
it_behaves_like 'a resourceful controller', User, formats: { index: [:html, :json] }
````That will test the following:
| Controller Method | request | Tests |
|-------------------|-----------|-------|
| `index` | `GET /` | The right number of objects is returned |
| `show` | `GET //:id` | The correct attributes are returned |You can also provide the json key value if your data is nested under that controller name:
````ruby
it_behaves_like 'a resourceful controller', User, formats: { index: [:html, :json] }, json_key: :users"users":
[
{ ... }
]
````