• Stars
    star
    919
  • Rank 47,939 (Top 1.0 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 13 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

Easily handle JSON in RSpec and Cucumber

json_spec

Easily handle JSON in RSpec and Cucumber

Gem Version Build Status Code Climate Dependency Status

RSpec

json_spec defines five new RSpec matchers:

  • be_json_eql
  • include_json
  • have_json_path
  • have_json_type
  • have_json_size

The new matchers could be used in RSpec as follows:

describe User do
  let(:user){ User.create!(first_name: "Steve", last_name: "Richert") }

  context "#to_json" do
    it "includes names" do
      names = %({"first_name":"Steve","last_name":"Richert"})
      user.to_json.should be_json_eql(names).excluding("friends")
    end

    it "includes the ID" do
      user.to_json.should have_json_path("id")
      user.to_json.should have_json_type(Integer).at_path("id")
    end

    it "includes friends" do
      user.to_json.should have_json_size(0).at_path("friends")

      friend = User.create!(first_name: "Catie", last_name: "Richert")
      user.friends << friend

      user.to_json.should have_json_size(1).at_path("friends")
      user.to_json.should include_json(friend.to_json)
    end
  end
end

json_spec also provides some useful helpers for RSpec tests:

  • parse_json
  • normalize_json
  • generate_normalized_json
  • load_json

To start using them add an include them in your RSpec configuration:

RSpec.configure do |config|
  config.include JsonSpec::Helpers
end

You can find usage examples for the helpers in spec/json_spec/helpers_spec.rb

Exclusions

json_spec ignores certain hash keys by default when comparing JSON:

  • id
  • created_at
  • updated_at

It's oftentimes helpful when evaluating JSON representations of newly-created ActiveRecord records so that the new ID and timestamps don't have to be known. These exclusions are globally customizeable:

JsonSpec.configure do
  exclude_keys "created_at", "updated_at"
end

Now, the id key will be included in json_spec's comparisons. Keys can also be excluded/included per matcher by chaining the excluding or including methods (as shown above) which will add or subtract from the globally excluded keys, respectively.

Paths

Each of json_spec's matchers deal with JSON "paths." These are simple strings of "/" separated hash keys and array indexes. For instance, with the following JSON:

{
  "first_name": "Steve",
  "last_name": "Richert",
  "friends": [
    {
      "first_name": "Catie",
      "last_name": "Richert"
    }
  ]
}

We could access the first friend's first name with the path "friends/0/first_name".

Cucumber

json_spec provides Cucumber steps that utilize its RSpec matchers and that's where json_spec really shines. This is perfect for testing your app's JSON API.

In order to use the Cucumber steps, in your env.rb you must:

require "json_spec/cucumber"

You also need to define a last_json method. If you're using Capybara, it could be as simple as:

def last_json
  page.source
end

Now, you can use the json_spec steps in your features:

Feature: User API
  Background:
    Given the following users exist:
      | id | first_name | last_name |
      | 1  | Steve      | Richert   |
      | 2  | Catie      | Richert   |
    And "Steve Richert" is friends with "Catie Richert"

  Scenario: Index action
    When I visit "/users.json"
    Then the JSON response should have 2 users
    And the JSON response at "0/id" should be 1
    And the JSON response at "1/id" should be 2

  Scenario: Show action
    When I visit "/users/1.json"
    Then the JSON response at "first_name" should be "Steve"
    And the JSON response at "last_name" should be "Richert"
    And the JSON response should have "created_at"
    And the JSON response at "created_at" should be a string
    And the JSON response at "friends" should be:
      """
      [
        {
          "id": 2,
          "first_name": "Catie",
          "last_name": "Richert"
        }
      ]
      """

The background steps above aren't provided by json_spec and the "visit" steps are provided by Capybara. The remaining steps, json_spec provides. They're versatile and can be used in plenty of different formats:

Then the JSON should be:
  """
  {
    "key": "value"
  }
  """
Then the JSON at "path" should be:
  """
  [
    "entry",
    "entry"
  ]
  """

Then the JSON should be {"key":"value"}
Then the JSON at "path" should be {"key":"value"}
Then the JSON should be ["entry","entry"]
Then the JSON at "path" should be ["entry","entry"]
Then the JSON at "path" should be "string"
Then the JSON at "path" should be 10
Then the JSON at "path" should be 10.0
Then the JSON at "path" should be 1e+1
Then the JSON at "path" should be true
Then the JSON at "path" should be false
Then the JSON at "path" should be null

Then the JSON should include:
  """
  {
    "key": "value"
  }
  """
Then the JSON at "path" should include:
  """
  [
    "entry",
    "entry"
  ]
  """

Then the JSON should include {"key":"value"}
Then the JSON at "path" should include {"key":"value"}
Then the JSON should include ["entry","entry"]
Then the JSON at "path" should include ["entry","entry"]
Then the JSON should include "string"
Then the JSON at "path" should include "string"
Then the JSON should include 10
Then the JSON at "path" should include 10
Then the JSON should include 10.0
Then the JSON at "path" should include 10.0
Then the JSON should include 1e+1
Then the JSON at "path" should include 1e+1
Then the JSON should include true
Then the JSON at "path" should include true
Then the JSON should include false
Then the JSON at "path" should include false
Then the JSON should include null
Then the JSON at "path" should include null

Then the JSON should have "path"

Then the JSON should be a hash
Then the JSON at "path" should be an array
Then the JSON at "path" should be a float

Then the JSON should have 1 entry
Then the JSON at "path" should have 2 entries
Then the JSON should have 3 keys
Then the JSON should have 4 whatevers

All instances of "should" above could be followed by "not" and all instances of "JSON" could be downcased and/or followed by "response."

Table Format

Another step exists that uses Cucumber's table formatting and wraps two of the above steps:

Then the JSON should have the following:
  | path/0 | {"key":"value"}   |
  | path/1 | ["entry","entry"] |

Any number of rows can be given. The step above is equivalent to:

Then the JSON at "path/0" should be {"key":"value"}
And the JSON at "path/1" should be ["entry","entry"]

If only one column is given:

Then the JSON should have the following:
  | path/0 |
  | path/1 |

This is equivalent to:

Then the JSON should have "path/0"
And the JSON should have "path/1"

JSON Memory

There's one more Cucumber step that json_spec provides which hasn't been used above. It's used to memorize JSON for reuse in later steps. You can "keep" all or a portion of the JSON by giving a name by which to remember it.

Feature: User API
  Scenario: Index action includes full user JSON
    Given the following user exists:
      | id | first_name | last_name |
      | 1  | Steve      | Richert   |
    And I visit "/users/1.json"
    And I keep the JSON response as "USER_1"
    When I visit "/users.json"
    Then the JSON response should be:
      """
      [
        %{USER_1}
      ]
      """

You can memorize JSON at a path:

Given I keep the JSON response at "first_name" as "FIRST_NAME"

You can remember JSON at a path:

Then the JSON response at "0/first_name" should be:
  """
  %{FIRST_NAME}
  """

You can also remember JSON inline:

Then the JSON response at "0/first_name" should be %{FIRST_NAME}

More

Check out the specs and features to see all the various ways you can use json_spec.

Contributing

If you come across any issues, please tell us. Pull requests (with tests) are appreciated. No pull request is too small. Please help with:

  • Reporting bugs
  • Suggesting features
  • Writing or improving documentation
  • Fixing typos
  • Cleaning whitespace
  • Refactoring code
  • Adding tests
  • Closing issues

If you report a bug and don't include a fix, please include a failing test.

Copyright

Copyright Β© 2011 Steve Richert

See LICENSE for details.

More Repositories

1

interactor

Interactor provides a common interface for performing complex user interactions.
Ruby
3,318
star
2

audited

Audited (formerly acts_as_audited) is an ORM extension that logs all changes to your Rails models.
Ruby
3,300
star
3

awesome_nested_set

An awesome replacement for acts_as_nested_set and better_nested_set.
Ruby
2,376
star
4

interactor-rails

Interactor Rails provides Rails support for the Interactor gem.
Ruby
434
star
5

delayed_job_active_record

ActiveRecord backend integration for DelayedJob 3.0+
Ruby
342
star
6

graticule

Graticule is a geocoding API for looking up address coordinates and performing distance calculations, supporting many popular APIs.
Ruby
299
star
7

tinder

Tinder is a Ruby API for interfacing with Campfire, the 37Signals chat application.
Ruby
258
star
8

inside_the_machine

HTML
255
star
9

acts_as_geocodable

Simple geocoding for Active Record models
Ruby
206
star
10

delayed_job_mongoid

Mongoid backend for delayed_job
Ruby
170
star
11

action_mailer_optional_tls

Enables TLS on SMTP connections (for services like GMail)
Ruby
127
star
12

command

Command provides a simple object-oriented interface for running shell commands from Ruby.
Ruby
93
star
13

sunspot_test

Auto-starts solr for your tests
Ruby
54
star
14

devise-mongo_mapper

MongoMapper ORM for Devise
Ruby
49
star
15

awesomeness

Collective Idea's Awesomeness. A collection of useful Rails bits and pieces.
Ruby
47
star
16

unicode_math

Fun Ruby extensions for doing math with Unicode
Ruby
47
star
17

acts_as_money

an Active Record plugin that makes it easier to work with the money gem
Ruby
45
star
18

calendar_builder

Ruby
45
star
19

caldav

A Ruby CalDAV client
Ruby
39
star
20

awesome-backup

Rails plugin that provides Rake and Capistrano tasks for making database backups
Ruby
32
star
21

merger

A gem for merging Active Record models
Ruby
30
star
22

html5

A Rails plugin for playing around with HTML5.
Ruby
29
star
23

with_action

A respond_to style helper for doing different actions based on which request parameters are passed.
Ruby
24
star
24

ARPlaneDetector

Use ARKit to find, and visualize horizontal and vertical planes.
Swift
23
star
25

CollectionGraph

Beautiful graphs made with UICollectionViews
Swift
23
star
26

rubymotion-parsedotcom-chat

A simple chat application written with RubyMotion and using Parse.com
Ruby
19
star
27

delayed_job_data_mapper

DataMapper backend for delayed_job
Ruby
16
star
28

deploy_and_deliver

Capistrano recipes for Pivotal Tracker. Can mark stories delivered on deploy.
Ruby
15
star
29

random_finders

A Rails plugin that allows quick and easy fetching of random records, or records in random order.
Ruby
14
star
30

searchparty

Simple site that aggregates Google, Delicious, and GitHub searches. See http://search.collectiveidea.com
JavaScript
13
star
31

pivotal_tracker

ActiveResource wrapper around v3 of the Pivotal Tracker API
Ruby
13
star
32

unifi

A ruby client for the (undocumented) Unifi AP Controller API
Ruby
12
star
33

measurement

A sweet Ruby measurement & converstion Library.
Ruby
12
star
34

clear_empty_attributes

When Active Record objects are saved from a form, empty fields are saved as empty strings instead of nil. This kills most validations.
Ruby
11
star
35

travis_bundle_cache

DEPRECATED: Cache the gem bundle for speedy travis builds (Travis now handles this)
Ruby
10
star
36

seinfeld.sh

Show the date of your last commit when logging into a terminal according to Calendar About Nothing
Shell
10
star
37

fireside

Open Source Chat
Ruby
10
star
38

twirp-kmm

Twirp service generator and runtime for Kotlin Multiplatform Mobile projects.
Kotlin
9
star
39

bender

Ruby
9
star
40

buildlight

Aggregating webhooks from multiple build services (Travis, Circle, Heroku) to power the stoplight in our office.
Ruby
9
star
41

tweet-wall

A Twitter visualizer
JavaScript
8
star
42

chat

An example chat application
Ruby
7
star
43

yardvote.com

Tracking political yard signs.
Ruby
7
star
44

simple-maps

Add Google Maps in seconds. Maps any and all hCards on the page and resizes the map to fit. Uses the new Google Maps JavaScript API v3. It will even do the geocoding for you!
JavaScript
7
star
45

metrics

A tool for tracking arbitrary metrics via Slack
Ruby
6
star
46

rails_relay_authentication

Ruby
6
star
47

capybara-ui

Page objects for readable, resilient acceptance tests
Ruby
6
star
48

rubymotion-drawing

Quick and dirty drawing app written with RubyMotion
Ruby
6
star
49

statsite-instrumental

Statsite Sink for Instrumental
Go
5
star
50

audit-demo

Demo application for acts_as_audited
5
star
51

migration_test_helper

Ruby
5
star
52

hourglass

Client and internal time management with Harvest
Ruby
5
star
53

acts_as_audited

acts_as_audited is now… Audited.
5
star
54

imap_authenticatable

Authenticate your Rails app using any IMAP server!
Ruby
5
star
55

rosie

CoffeeScript
4
star
56

playing-with-sunspot

A demo of sunspot and solr
Ruby
4
star
57

perfecttime

Mike West's PerfectTime.js, with a few updates.
JavaScript
4
star
58

ember-helpers

A collection of handlebars helpers for Ember.js
JavaScript
4
star
59

git-access

Secure access to hosted git repositories
Go
4
star
60

pivotalharvest

Ruby
4
star
61

pgbackups_archive_app

A very simple app to archive PGBackups to S3.
Ruby
4
star
62

recursable

Some helpers for using trees in Rails
Ruby
3
star
63

integrity-nabaztag

An Integrity notifier for Nabaztag
Ruby
3
star
64

adapter-simpledb

An adapter for Amazon's SimpleDB
Ruby
3
star
65

data-loss

Tracking when things happened.
Ruby
3
star
66

eharbor

Example Rails application for Ruby on Rails training used by Collective Idea and Idea Foundry (http://ideafoundry.info/ruby-on-rails). Note: we often use interactive rebases, so commit dates may not make sense.
Ruby
3
star
67

ios-swifter-example

Using Swifter to stub network calls
Swift
3
star
68

twitter

A Twitter clone used for training
Ruby
2
star
69

collective-data

Gem for pulling data from the CollectiveData website
Ruby
2
star
70

get-to-work-go

Start and stop project specific, annotated Harvest timers with information from Pivotal Tracker
Go
2
star
71

dm-mongo-adapter

2
star
72

example_of_sunspot_test

A rails 3 codebase showing the usage of sunspot_test
Ruby
2
star
73

jquery.defaulttext

jQuery plugin to set the default text of an input using the title element
JavaScript
2
star
74

over_board

Ruby
2
star
75

test-kitchen-provisioners

A collection of test-kitchen provisioners (https://github.com/opscode/test-kitchen)
Ruby
2
star
76

namecase

Ruby
2
star
77

sunspot-indexing-strategies

A Rails 3 codebase that uses Sunspot to show how you can search search within words.
Ruby
2
star
78

mephisto-comment-notification

A simple comment notification plugin for the Mephisto blogging engine.
Ruby
2
star
79

mintastic

A Mint to QIF converter that also functions as a Rails + React + Redux tutorial app
Ruby
2
star
80

react-ujs-rails

React UJS package for rails, extracted from react-rails
JavaScript
1
star
81

mephisto_atompub

AtomPub plugin for mephisto
Ruby
1
star
82

donuts-android

Android client for donuts.collectiveidea.com
Kotlin
1
star
83

cucumber-web

Reusable web steps for Cucumber and Capybara
Ruby
1
star
84

laketown

Calculate what you could save on internet service in Laketown Township
HTML
1
star
85

cucumber_slices

Cucumber steps that we use often
Ruby
1
star
86

donuts

Collective Idea Donut Tracker
Ruby
1
star
87

donuts-ios

iOS Client for donuts.collectiveidea.com
Swift
1
star
88

rubocop-config

Working toward a rubocop config we all can agree on.
1
star
89

build-test

Stupid simple way to test a continuous integration build server.
Shell
1
star
90

get_to_work

A rubygem to tag your Harvest time entries with Pivotal Tracker Information
Ruby
1
star
91

no-phone

Not a phone.
Ruby
1
star
92

delayed_job_example

An example app for using Delayed Job
Ruby
1
star
93

finish_weekend

Finish Weekend
Ruby
1
star
94

twirp-rails

Make serving a Twirp RPC Services as easy and familiar as Rails controllers.
Ruby
1
star