I'm using the "json_spec" gem in a Rails app and I'm trying to create some Cucumber steps to document/test my API code.
I have the following scenario:
Scenario: Create new authorization
Given an existing user and merchant
When I post to "/api/v1/authorizations.json" with:
"""
{"email":"user@email.com", "code": "43434", "merchant_account_id":1, "amount": 45.00}
"""
And I keep the JSON response at "authorization/id" as "AUTH_ID"
Then the JSON response should be:
"""
{"authorization":{"id":1,"email":"user@email.com","amount":45.0}}
"""
I expected "authorization/id" to give me the value of the "id" key in the "authorizations" hash. All the examples in the json_spec documentation contain arrays, but in this case the response only involves one entity--it would not make sense to have an array in the JSON. Is there a way to use paths when there is no array present?
Sure, and the syntax you used is correct. The scenarios in the documentation examples work with a response to a post, and then a check whether that post is part of the collection - hence the array. But if you were to check e.g., that your post now is the last post (for which you retrieve just one value) you'd use the hash syntax:
Scenario: Create new authorization
Given an existing user and merchant
And I post to "/api/v1/authorizations.json" with:
"""
{"email":"user@email.com", "code": "43434", "merchant_account_id":1, "amount": 45.00}
"""
And I keep the JSON response at "authorization/id" as "AUTH_ID"
When I get from "/api/v1/authorizations/last.json"
Then the JSON response at "authorization/id" should be %{AUTH_ID}