• Stars
    star
    18
  • Rank 1,208,065 (Top 24 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

RSpec matchers for JSON strings with the power of built-in matchers and composable matchers

Gem Version Test Coverage Status Code Climate

RSpec::ComposableJSONMatchers

RSpec::ComposableJSONMatchers provides be_json matcher, which lets you express expected structures on JSON strings with the power of RSpec's built-in matchers and composable matchers.

json = '{ "foo": 1, "bar": 2 }'
expect(json).to be_json a_kind_of(Hash)
expect(json).to be_json matching(foo: 1, bar: a_kind_of(Integer))
expect(json).to be_json including(foo: 1)

Installation

Adding rspec-composable_json_matchers dependency

Add this line to your Gemfile:

gem 'rspec-composable_json_matchers'

And then run:

$ bundle install

Enabling be_json matcher

To make the be_json matcher available in every example, add the following line to your spec/spec_helper.rb:

# spec/spec_helper.rb
require 'rspec/composable_json_matchers/setup'

Or if you prefer more explicit way, add the following snippet:

# spec/spec_helper.rb
require 'rspec/composable_json_matchers'

RSpec.configure do |config|
  config.include RSpec::ComposableJSONMatchers
end

If you want to enable the be_json matcher only specific examples rather than every example, include the RSpec::ComposableJSONMatchers in the example groups:

# spec/something_spec.rb
require 'rspec/composable_json_matchers'

describe 'something' do
  include RSpec::ComposableJSONMatchers
end

Usage

The be_json matcher takes another matcher or a JSON value (hash, array, numeric, string, true, false, or nil).

When a matcher is given, it passes if actual string can be decoded as JSON and the decoded value passes the given matcher:

expect('{ "foo": 1, "bar": 2 }').to be_json a_kind_of(Hash)
expect('{ "foo": 1, "bar": 2 }').to be_json matching(foo: 1, bar: 2)
expect('{ "foo": 1, "bar": 2 }').to be_json including(foo: 1)

expect('["foo", "bar"]').to be_json a_kind_of(Array)
expect('["foo", "bar"]').to be_json matching(['foo', 'bar'])
expect('["foo", "bar"]').to be_json including('foo')

expect('null').to be_json(nil)

When a JSON value is given, it's handled as be_json matching(value) (matching is an alias of the match matcher):

# Equivalents
expect('{ "foo": 1, "bar": 2 }').to be_json(foo: 1, bar: 2)
expect('{ "foo": 1, "bar": 2 }').to be_json matching(foo: 1, bar: 2)

You can compose matchers via given matchers:

expect('{ "foo": 1, "bar": 2 }').to be_json matching(
  foo: a_kind_of(Integer),
  bar: a_kind_of(Integer)
)

expect('{ "foo": 1, "bar": 2 }').to be_json including(foo: a_kind_of(Integer))

For more practical example, see spec/example_spec.rb for the GitHub Meta API.

Combinations with built-in matchers

Since decoded JSON is a hash or an array in most cases, you may want to use any of the following built-in matchers.

Note that you can always use the match matcher (internally uses #===) instead of the eq matcher (internally uses #==), because there's no object that is parsed from JSON and behaves differently between #== and #===.

Of course, any other custom matchers can also be used.

Also, using the built-in matcher aliases is recommended since it reads well:

# Equivalents
expect('{ "a": "foo" }').to be_json matching(a: a_string_including('oo')) # Matcher aliases
expect('{ "a": "foo" }').to be_json match(a: include('oo')) # Original matcher names

matching

  • Alias of match matcher
  • Supported structure: Hash and Array
  • Accepts matchers as arguments: Yes
expect('{ "foo": 1, "bar": 2 }').to be_json matching(foo: 1, bar: a_kind_of(Integer))
expect('["foo", "bar"]').to be_json matching(['foo', a_string_starting_with('b')])

including

  • Alias of include matcher
  • Supported structure: Hash and Array
  • Accepts matchers as arguments: Yes
expect('{ "foo": 1, "bar": 2 }').to be_json including(foo: 1)
expect('["foo", "bar"]').to be_json including('foo')

all

  • all matcher
  • Supported structure: Array
  • Accepts matchers as arguments: Yes
expect('["foo", "bar"]').to be_json all be_a(String)

containing_exactly

  • Alias of contain_exactly matcher
  • Supported structure: Array
  • Accepts matchers as arguments: Yes
expect('["foo", "bar"]').to be_json containing_exactly('bar', 'foo')

starting_with

  • Alias of start_with matcher
  • Supported structure: Array
  • Accepts matchers as arguments: Yes
expect('["foo", "bar"]').to be_json starting_with('foo')

ending_with

  • Alias of end_with matcher
  • Supported structure: Array
  • Accepts matchers as arguments: Yes
expect('["foo", "bar"]').to be_json ending_with('bar')

having_attributes

  • Alias of have_attributes matcher
  • Supported structure: Hash and Array
  • Accepts matchers as arguments: Yes
expect('{ "foo": 1, "bar": 2 }').to be_json having_attributes(keys: [:foo, :bar])
expect('["foo", "bar"]').to be_json having_attributes(size: 2)

a_kind_of

  • Alias of be_a_kind_of matcher
  • Supported structure: Hash and Array
  • Accepts matchers as arguments: No
expect('{}').to be_json a_kind_of(Hash)
expect('[]').to be_json a_kind_of(Array)

Configuration

The be_json matcher internally uses JSON.parse to decode JSON strings. The default parser options used in the be_json matcher is { symbolize_names: true }, so you need to pass a hash with symbol keys as an expected structure.

If you prefer string keys, add the following snippet to your spec/spec_helper.rb:

# spec/spec_helper.rb
RSpec::ComposableJSONMatchers.configure do |config|
  config.parser_options = { symbolize_names: false }
end

License

Copyright (c) 2016 Yuji Nakayama

See the LICENSE.txt for details.

More Repositories

1

transpec

The RSpec syntax converter
Ruby
1,011
star
2

NAKPlaybackIndicatorView

A UIView that mimics the music playback indicator in the Music.app on iOS 7+
Objective-C
399
star
3

atom-lint

Obsolete: Generic code linting support for Atom
CoffeeScript
112
star
4

astrolabe

An object-oriented Ruby AST extension for Parser
Ruby
67
star
5

rspec-hue_formatter

Bring RSpec integration into your room with Philips Hue
Ruby
59
star
6

atom-auto-update-packages

Keep your Atom packages up to date.
CoffeeScript
33
star
7

apple-music-node

Node.js Client for Apple Music built with TypeScript
TypeScript
31
star
8

homebridge-switchbot

Homebridge plugin for SwitchBot
JavaScript
29
star
9

safedep

Make your Gemfile safe by adding dependency version specifiers automatically
Ruby
12
star
10

pacman-repo

My personal pacman repository
Perl
6
star
11

cocoapods-check_latest

A CocoaPods plugin that checks if the latest version of a pod is up to date.
Ruby
6
star
12

alfred-itunes-airplay

iTunes AirPlay Control for Alfred
Objective-C
5
star
13

smart-car-dashboard

Smart car dashboard, built for my Subaru Levorg with iPad Pro, ESP32, and Firebase
Swift
5
star
14

repository_merger

A tool for merging existing Git repositories into a monorepo while keeping their commit history
Ruby
4
star
15

dotfiles

My personal dotfiles
Shell
3
star
16

gemologist

A library for rewriting your Gemfile and gemspec
Ruby
1
star
17

japan_etc

Japanese ETC (Electronic Toll Collection System) tollbooth database / ETC料金所番号データベース
Ruby
1
star
18

zsh-completions-osx

Zsh completion scripts for Mac OS X specific tools.
1
star
19

rubygems-xcodeproj_generator

Provides a Rake task for generating an Xcode project for Ruby C extension development.
Ruby
1
star