Git Product home page Git Product logo

response_matcher's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.