No commit activity in last 3 years
No release in over 3 years
Generate fixtures from JSON schemas.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0
>= 0

Runtime

 Project Readme

##JsonSchemaSpec

Generate fixtures from JSON schemas

Build Status

###Goals

  • Generate webmock requests and responses from JSON schema
  • Easily alter parameters for testing
  • Automatically validate altered parameters against schema
  • Rake helpers for client code to download JSON schema

###Requirements

You serve a schema.json file at the root of a URL that describes your resources.

####Example schema.json

{
    "user.json": {
        "get": {
            "request": {
                "title": "GET user.json (request)",
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "id":    { "type": "integer" },
                    "token": { "type": "string" }
                }
            },
            "response": {
                "title": "GET user.json (response)",
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "id":     { "type": "integer" },
                    "avatar": { "type": "string", "optional": true },
                    "name":   { "type": "string" }
                }
            }
        }
    }
}

###Install

gem install json_schema_spec

###Require

In your spec_helper:

require "json_schema_spec"

###Client side project setup

In your Rakefile:

require "json_schema_spec"
JsonSchemaSpec::Tasks.new("http://127.0.0.1:3000/schema.json")

####Download schema

Download schema.json from the URL specified in your Rakefile:

rake spec:schema

The schema lives at schema/fixtures/schema.yml.

###Server side project setup

In Rails, your schema is automatically detected at /schema.json.

###Generate test parameters

request, response = json_schema_params(:user, :get)

The request hash looks like this:

{ :id => 123, :token => "token" }

The response hash looks like this:

{ :avatar => "avatar", :name => "name" }

Integer values are given the value 123.

String values are given the same name as the key.

Optional parameters are not included.

###Modify test parameters

request, response = json_schema_params(:user, :get, :request => { :id => 1 })

The request hash now looks like this:

{ :id => 1, :token => "token" }

Modified parameters are validated against your schema using json-schema.

###Webmock example

request, response = json_schema_params(:user, :get)

request  = params[:request]
response = params[:response]

stub_request(:get, "http://127.0.0.1:3000/user.json").
  with(:body => request).
  to_return(:body => response.to_json)

Contribute

Create an issue to discuss template changes.

Pull requests for template changes and new branches are even better.

Stay up to date

Star this project on Github.

Follow Winton Welsh on Twitter.