No commit activity in last 3 years
No release in over 3 years
Rspec matchers for APIs
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
~> 10.0

Runtime

 Project Readme

Rspec::ApiHelpers

Usefull Rspec helpers for APIs (currently JSONAPI and AM-JSON adapters are supported)

Installation

Add this line to your application's Gemfile:

gem 'rspec-api_helpers', '1.0.3'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rspec-api_helpers

Usage

This Gem expects you to have set your rspec to use Rake::Test helpers as described here because it checks last_response attributes.

In your rails_helper.rb add in the top:

require 'rspec/api_helpers'

Then you need to specify the adapter you want.

RSpec.configure do |config|
  config.include Rspec::ApiHelpers.with(adapter: :json_api)
end

Other possible options for the adapter is active_model.

You can also inject your custom made class by providing the class:

RSpec.configure do |config|
  config.include Rspec::ApiHelpers.with(adapter: Adapter::Hal)
end

Examples

The library heavily uses dynamic scopes through procs (an alternative to eval).

General

it_returns_status(200)

It expects the HTTP response status to be 200.

it_includes_in_headers('SESSION_TOKEN' => proc{@user.token})

It expects the HTTP response to include this header (and value).

Resource

it_returns_attributes(resource: 'user', model: proc{@user}, attrs: [
  :email, :name
])

It expects the JSON resource to contain :email and :name attributes and be equal with with @user methods.

it_returns_attribute_values(
  resource: 'user', model: proc{@user}, attrs: [
    :id, :name, :email, :admin, :activated, :created_at, :updated_at
  ], modifiers: {
    [:created_at, :updated_at] => proc{|i| i.iso8601},
    :id => proc{|i| i.to_s}
  }
)

It expects the JSON resource to contain specific attribute values as above but for :updated_at, :created_at and id it applies specific methods first, defined in the modifier hash (note that the methods are applied in the JSON resource, not in the variable)

it_returns_no_attributes(
  resource: 'user', attrs: [:foo1, :foo2, :foo3]
)

It expects the JSON resource to NOT contain any of those attributes.

Collection

it_returns_collection_size(resource: 'users', size: 6)

It expects the JSON collection to have 6 resources.

it_returns_collection_attributes(
  resource: 'users', attrs: [
    :id, :name, :email, :admin, :activated, :created_at, :updated_at
  ]
)

It expects the JSON collection to have those attributes (no value checking).

it_returns_no_collection_attributes(
  resource: 'users', attrs: [
    :foo
  ]
)

It expects the JSON collection NOT to have those attributes (no value checking).

it_returns_collection_attribute_values(
  resource: 'users', model: proc{User.first!}, attrs: [
    :id, :name, :email, :admin, :activated, :created_at, :updated_at
  ], modifiers: {
    [:created_at, :updated_at] => proc{|i| i.iso8601},
    :id => proc{|i| i.to_s}
  }
)

It expects the JSON collection to contain specific attribute values as above but for :updated_at, :created_at and id it applies specific methods first, defined in the modifier hash (note that the methods are applied in the JSON resource, not in the variable)

To Do

  • Enhance documentation
  • Better dir structure (break example group methods to modules)
  • Support for nested resources
  • Add tests
  • Support for HAL/Siren adapter

Contributing

  1. Fork it ( https://github.com/[my-github-username]/rspec-api_helpers/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request