{"id":15175177,"url":"https://github.com/dogparklabs/json_rspec_match_maker","last_synced_at":"2025-10-01T12:31:03.266Z","repository":{"id":56879613,"uuid":"124688500","full_name":"DogParkLabs/json_rspec_match_maker","owner":"DogParkLabs","description":"Utility class for making JSON matchers for RSpec. ","archived":true,"fork":false,"pushed_at":"2020-02-29T10:57:06.000Z","size":48,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":15,"default_branch":"master","last_synced_at":"2024-09-24T05:31:42.376Z","etag":null,"topics":["json","rspec","ruby-on-rails","testing"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DogParkLabs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-10T18:48:56.000Z","updated_at":"2023-01-28T06:06:32.000Z","dependencies_parsed_at":"2022-08-20T11:40:42.463Z","dependency_job_id":null,"html_url":"https://github.com/DogParkLabs/json_rspec_match_maker","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DogParkLabs%2Fjson_rspec_match_maker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DogParkLabs%2Fjson_rspec_match_maker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DogParkLabs%2Fjson_rspec_match_maker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DogParkLabs%2Fjson_rspec_match_maker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DogParkLabs","download_url":"https://codeload.github.com/DogParkLabs/json_rspec_match_maker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219875425,"owners_count":16554677,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","rspec","ruby-on-rails","testing"],"created_at":"2024-09-27T12:04:33.990Z","updated_at":"2025-10-01T12:31:02.974Z","avatar_url":"https://github.com/DogParkLabs.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JsonRspecMatchMaker\n\nHelper class for writing custom RSpec matchers for JSON api endpoints.\n\nDRY up API expectations, without losing the specificity sacrificed by a\nschema-based approach.\n\n## Installation with Rails\n\nAdd this line to your application's Gemfile:\n\n```\ngem 'json_rspec_match_maker', require: false\n```\n\nAnd then execute:\n\n    $ bundle\n    \nUpdate your `rails_helper.rb` with:\n\n```\n# require the gem\nrequire 'json_rspec_match_maker'\n\n# require your custom matchers you'll be writing\nDir[Rails.root.join('spec/support/matchers/json_matchers/**/*.rb')].each do |f|\n  require f\nend\n```\n\n## Usage\n\nMatchers are instantiated with some object and a match definition.\n\nIf our address class looks like:\n\n```\nclass Address\n  attr_reader :street1, :street2, :city, :state, :zip\nend\n```\n\nAnd we serialize that into JSON like:\n\n```\n{\n  address: {\n    street1: '18 Streety Street',\n    street2: 'APT 22B',\n    city: 'Citytown',\n    state: 'NY',\n    zip: '11111'\n  }\n}\n```\n\nThen we can define a matcher like:\n\n```\nmodule JsonMatchers\n  def be_valid_json_for_address(address)\n    JsonRspecMatchMaker::Base.new(address, %w[street1 street2 city state zip], prefix: 'address')\n  end\nend\n```\n\nAnd use it in a spec like:\n\n```\ndescribe 'my api', type: :request do\n  let(:test_address) { Address.new('Street', '2', 'Place', 'ZZ', '00000') }\n  it 'gets some stuff' do\n    get '/api/address'\n    expect(JSON.parse(response.body)).to be_valid_json_for_address(test_address)\n  end\nend\n```\n\nYou can also pass a custom Proc to override the methods called to fetch the value for a key:\n(these need to be last in the array - taking advantage of ruby sytanx sugar omitting surrounding brackets)\n\n```\nmatch = [\n  'id',\n  'name' =\u003e -\u003e(object) { object.full_name }\n]\n```\n\nSingle nested objects are simple:\n\n```\nmatch = [\n  'user.address.street1'\n]\n```\n\nNested lists have one extra step\n:each should be a proc that fetches the list to iterate through\n:attributes should be another definition, following same rules as the outer one\n\n```\nmatch = [\n  'id',\n  'name',\n  'photographs_attributes' =\u003e {\n    each: -\u003e (object) { object.photographs.visible },\n    attributes: %w[id caption url]\n  }\n]\n```\n\nYou may want to break out more complicated definitions into their own class:\n\n```\nmodule JsonMatchers\n  class AddressMatcher \u003c JsonRspecMatchMaker::Base\n    MATCH = %w[street1 street2 city state zip].freeze\n    \n    def initialize(address)\n      super(address, MATCH, prefix: 'address')\n    end\n  end\n\n  def be_valid_json_for_address(address)\n    AddressMatcher.new(address)\n  end\nend\n```\n\nYou might want your matchers to be more dynamic so you could do something like:\n\n```ruby\nclass AddressMatcher \u003c JsonRspecMatchMaker::Base\n  def initialize(address, state_format)\n    match_definition = set_match_def(state_format)\n    super(address, match_definition)\n  end\n  \n  def set_match_def(state_format)\n    [\n      'state' =\u003e -\u003e(instance) { instance.state.formatted(state_format) }\n    ]\n  end\nend\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/json_rspec_match_maker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the JsonRspecMatchMaker project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/json_rspec_match_maker/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdogparklabs%2Fjson_rspec_match_maker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdogparklabs%2Fjson_rspec_match_maker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdogparklabs%2Fjson_rspec_match_maker/lists"}