collectiveidea / json_spec

Easily handle JSON in RSpec and Cucumber
rubygems.org/gems/json_spec
MIT License
919 stars 114 forks source link

Quotes within JSON body should not be memorised with escaped double quotes #51

Closed vinayvinay closed 11 years ago

vinayvinay commented 11 years ago
When I get "/files.json"
# my last_json returns something like:
# "{\"files\":[{\"uuid\":\"7cd19c4d-83c2-4acb-9ed8-44800eee6442\"},{\"uuid\":\"05a8bee9-c7b0-43d8-9aa2-743f5786deb8\"]}"

And I keep the JSON response at "files/0/uuid" as "FILE_UUID"
# this is was memorized as: {:IMAGE_UUID=>"\"7cd19c4d-83c2-4acb-9ed8-44800eee6442\""}
# would expect just "7cd19c4d-83c2-4acb-9ed8-44800eee6442", which is without the escaped strings

When I get "/files/%{FILE_UUID}.json"
# because the above actual memorisation results in a get to url "/files/\"7cd19c4d-83c2-4acb-9ed8-44800eee6442\".json", which is not right
laserlemon commented 11 years ago

The typical usage for this is to assert equality of subsections of JSON, not to pull out Ruby values. While I see that this would be useful, I wouldn't want to change the normal memory so drastically. You could change your step to something like:

step /I get "([^"]*)"/ do |path|
  actually_perform_get parse_json(JsonSpec.remember(path))
end

…assuming you have the JsonSpec::Helpers included.

I hope that helps. Please feel free to submit a pull request to add something like JsonSpec::Memory#remember_and_parse.