{"id":18103003,"url":"https://github.com/sznagymisu/json_response_matchers","last_synced_at":"2026-05-07T06:34:47.439Z","repository":{"id":56879585,"uuid":"167984220","full_name":"SzNagyMisu/json_response_matchers","owner":"SzNagyMisu","description":"rspec matchers to test http responses with json content in rails","archived":false,"fork":false,"pushed_at":"2019-01-31T16:49:25.000Z","size":15,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-03-24T09:03:13.899Z","etag":null,"topics":["controller-specs","json","rails","request-specs","response","rspec"],"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/SzNagyMisu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-01-28T15:21:26.000Z","updated_at":"2019-04-30T07:24:59.000Z","dependencies_parsed_at":"2022-08-20T11:40:40.015Z","dependency_job_id":null,"html_url":"https://github.com/SzNagyMisu/json_response_matchers","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SzNagyMisu%2Fjson_response_matchers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SzNagyMisu%2Fjson_response_matchers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SzNagyMisu%2Fjson_response_matchers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SzNagyMisu%2Fjson_response_matchers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SzNagyMisu","download_url":"https://codeload.github.com/SzNagyMisu/json_response_matchers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247441007,"owners_count":20939235,"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":["controller-specs","json","rails","request-specs","response","rspec"],"created_at":"2024-10-31T22:09:51.358Z","updated_at":"2026-05-07T06:34:47.377Z","avatar_url":"https://github.com/SzNagyMisu.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JsonResponseMatchers\n\n[rspec](https://relishapp.com/rspec) matchers for testing json responses in a [rails](https://rubyonrails.org/) application.\n\n## Dependencies\n\n* **ruby** `'\u003e= 2.0.0'`\n* **rspec** `'\u003e= 3.0'` \u003c!-- see composable matchers --\u003e\n* **activesupport** `'\u003e= 4.0.6'` \u003c!-- see https://github.com/rails/rails/pull/10887 --\u003e\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngroup :test do\n  gem 'json_response_matchers'\nend\n```\n\nAnd then execute:\n\n    $ bundle\n\n## Usage\n\n1. include the matchers in your rspec config\n\n  ```ruby\n  # spec/rails_helper.rb\n  RSpec.configure do |config|\n    # you can include it in a general way\n    config.include JsonResponseMatchers\n    # but it is recommended to include it only for the request\n    config.include JsonResponseMatchers, type: :request\n    # or the request and controller specs\n    config.include JsonResponseMatchers, type: /request|controller/\n  end\n  ```\n\n2. write more concise request specs\n\n  ```ruby\n  # in your request spec examples\n  # instead of\n  item = JSON.parse(response.body)['item']\n  expect(item['name']).to eq 'item-name'\n  # use the #have_json_content matcher\n  expect(response).to have_json_content('item-name').at_key :item, :name\n\n  # instead of\n  items = JSON.parse(repsonse.body)['items']\n  expect(items.map { |item| item['id'] }).to match_array [ 1, 2, 3, 4, 5 ]\n  # use the #have_json_values matcher\n  expect(response).to have_json_values(1, 2, 3, 4, 5).for(:id).at_key :items\n  ```\n\n### general rules\n\nThe target of the matchers can be a json parsable string or an object with such a string at the method `#body` (like the test response in rails request tests).\n\n```ruby\nexpect('{\"item\":{\"id\":1,\"name\":\"item-name\"}}').to have_json_content('item-name').at_key :item, :name\n# or if `response.body` contains the above json\nexpect(response).to have_json_content('item-name').at_key :item, :name\n```\n\nThe `#at_key` method is optional and can receive one or more `string`, `symbol` or `integer` keys.\n\n```ruby\nexpect('false').to have_json_content(false)\n\njson = '{\"items\":[{\"id\":1,\"name\":\"item-1\"},{\"id\":2,\"name\":\"item-2\"}]}'\nexpect(json).to have_json_values(1, 2).for(:id).at_key 'items'\nexpect(json).to have_json_values(1, 2).for(:id).at_key :items\nexpect(json).to have_json_content('item-2').at_key :items, 1, 'name'\n```\n\nBoth matchers are [composable](https://relishapp.com/rspec/rspec-expectations/docs/composing-matchers).\n\n```ruby\nexpect('{\"item\":{\"id\":1},\"user\":{\"name\":\"user\"}}')\n  .to have_json_content(1).at_key(:item, :id)\n  .and have_json_values('user').at_key(:user, :name)\n\nexpect('{\"items\":[{\"id\":1},{\"id\":2}],\"page\":1}')\n  .to have_json_values(1, 2).for(:id).at_key(:item)\n  .and have_json_content(1).at_key(:page)\n```\n\n### #have_json_content\n\nChecks single values.\n\n* If expected value is not a `hash`, it checks equality\n\n  ```ruby\n  # all pass\n  expect('{\"string\":\"string\"}').to have_json_content('string').at_key :string\n  expect('{\"number\":123}').to have_json_content(123).at_key :number\n  expect('{\"boolean\":false}').to have_json_content(false).at_key :boolean\n  expect('{\"array\":[\"a\",2]}').to have_json_content([ 'a', 2 ]).at_key :array\n  expect('{\"null\":null}').to have_json_content(nil).at_key :null\n  ```\n\n* If expected value is a `hash`\n  * and `#with_full_match` is specified, it checks equality\n  * otherwise it checks inclusion\n  * accepts symbol keys\n\n    ```ruby\n    expect('{\"id\":1,\"name\":\"i1\"}').to have_json_content('id'=\u003e1).with_full_match # fails\n    expect('{\"id\":1,\"name\":\"i1\"}').to have_json_content('id'=\u003e1) # passes\n    expect('{\"id\":1,\"name\":\"i1\"}').to have_json_content(name: 'i1', id: 1).with_full_match # passes\n    ```\n\n\n### #have_json_values\n\nChecks arrays. The expected values are passed as a parameter list (`*args`) to the matcher.\n\n* the metod `#for` is required and can take a `string` or `symbol` value\n\n  ```ruby\n  items = [\n    { id: 1, name: 'item-1' },\n    { id: 2, name: 'item-2' },\n    { id: 3, name: 'item-3' }\n  ]\n  expect(items.to_json).to have_json_values(1, 2, 3).for('id') # passes\n  expect(items.to_json).to have_json_values(1, 2, 3).for(:id) # passes\n  expect(items.to_json).to have_json_values(*items) # fails with ArgumentError\n  ```\n\n* checks order only if `#in_strict_order` is specified\n\n  ```ruby\n  expect(items.to_json).to have_json_values(1, 3, 2).for(:id) # passes\n  expect(items.to_json).to have_json_values(1, 3, 2).for(:id).in_strict_order # fails\n  ```\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/SzNagyMisu/json_response_matchers.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsznagymisu%2Fjson_response_matchers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsznagymisu%2Fjson_response_matchers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsznagymisu%2Fjson_response_matchers/lists"}