Test your JSON in RSpec and Cucumber, like a boss.
The only matcher I let my kids play with -- Mom
Ever since the dawn of mankind, humans everywhere have been searching for a tool to test their JSON.
Then computers were invented.
Then matchJSON came about.
Then everyone lived happily ever after.
Stop warping your brain with hard-to-read regex and weird-looking tests.
The old, yucky way:
it "returns the current pony" do
get "/ponies/#{pony.id}"
current_pony = JSON.parse(response.body)["pony"]
expect(current_pony["cuteness"]).to eq 90
expect(current_pony["fluffiness"]).to eq "extra-fluffy"
expect(current_pony["name"]).to eq "McPony"
end
The new, cool way:
it "returns the current pony" do
get "/ponies/#{pony.id}"
expect(response).to match_json(<<-JSON)
{
"cuteness": 90,
"fluffiness": "extra-fluffy",
"name": "McPony",
}
JSON
end
Wait, you already use JSON-Schema? We're BFFs ❤️
it "returns the current pony" do
get "/ponies/#{pony.id}"
# JSON-Schema to check the schema
expect(response).to match_response_schema(:pony)
# MatchJSON to test the values (gotta love explicit tests)
expect(response).to match_json(<<-JSON)
{
"cuteness": 90,
"fluffiness": "extra-fluffy",
"name": "McPony",
}
JSON
end
- Drop this baby into your Gemfile like so:
gem 'match_json'
- Run
bundle
Add some patterns in the mix to make things interesting:
# RSpec
it 'returns ponies' do
get '/ponies/#{pony.id}'
expect(response).to match_json(<<-JSON)
{
"id": "{uuid}", # UUID Pattern
"cuteness": 90,
"fluffiness": "extra-fluffy",
"name": "McPony",
"created_at": "{date_time_iso8601}" # DateTime pattern (well, duh)
}
JSON
end
Here's a list of the built-in patterns we provide:
date_time_iso8601
date
uuid
email
string
Just add them to spec/support/match_json.rb
:
MatchJson::Matchers::IncludeJson::PATTERNS['id'] = /\A\d{6}\z/
and then use it in your spec like so:
it 'uses patten to check value' do
expect(%Q({"one": "123456"})).to match_json(%Q({"one": "{id}"}))
# .. you can even do this:
expect(%Q({"one": 123456})).to match_json(%Q({"one": {id}}))
end
- Fork it ( https://github.com/[my-github-username]/match_json/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request