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

Development

~> 1.14
~> 0.10.4
~> 12.0
>= 3.0
~> 0.50.0

Runtime

 Project Readme

ResponseMatcher

Gem Version CircleCI

Solution for matching JSON response into RSpec request tests.

Installation

Add this line to your application's Gemfile:

gem 'response_matcher'

And then execute:

$ bundle

Or install it yourself as:

$ gem install response_matcher

Usage

Quick start

In registration_spec.rb

describe RegistrationsController do
  describe 'POST /auth' do
    it 'response' do
      post user_registration_path, params: params

      expect(response).to response_match('schema_path', user: User.last)
    end
  end
end

You can create your own schema

└─ spec
    ├─ schemas
    │   ├─ my_schema.rb
    │   └─ ...
    └─ ...

In my_schema.rb you can describe by ruby language how looks like your response

# All the parameters that you transmit will be available as variables, like 'user'
{
  data: {
    id: user.id,
    type: user.class.name,
    attributes: {
      email: user.email,
      provider: user.provider,
      uid: user.uid
    }
  }
}

You can reuse your schemas in another schemas

└─ spec
    ├─ schemas
    │   ├─ my_schema.rb
    │   ├─ attributes.rb
    │   └─ ...
    └─ ...

In my_schema.rb

# All the parameters that you transmit will be available as variables, like 'user'
{
  data: {
    id: user.id,
    type: user.class.name,
    attributes: call('attributes', user: user)
  }
}

In attributes.rb

{
  email: user.email,
  provider: user.provider,
  uid: user.uid
}

You can use helpers in your schemas:

└─ spec
    ├─ schemas
    │   ├─ my_schema.rb
    │   ├─ attributes.rb
    │   └─ ...
    ├─ helpers
    │   ├─ my_helper.rb
    │   └─ ...
    ├─ rails_helper.rb
    └─ ...

In my_helper.rb

module Helpers
  module MyHelper
    def entity_class(entity)
      entity.class.name
    end
  end
end

In rails_helper.rb

ResponseMatcher::Settings.configure do |config|
  config.helpers = [
    Helpers::MyHelper,
    ...
  ]
end

In my_schema.rb

{
  data: {
    id: user.id,
    type: entity_class(user),
    attributes: call('attributes', user: user)
  }
}

You can set base directory of your schemas:

In rails_helper.rb

ResponseMatcher::Settings.configure do |config|
  config.directory = 'spec/schemas/api/v1'
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/bezrukavyi/response_matcher

License

The gem is available as open source under the terms of the MIT License.