Filling In the Response Body

Think about what you’d like the response data to look like. It’d be nice to get back a unique ID for the expense we just recorded so that we can refer to it later. Let’s return a JSON object that looks something like this:

 { "expense_id": 42 }

Ruby’s JSON library can safely parse simple a record like this into a Ruby hash:

 >>​ require ​'json'
 => true
 >>​ JSON.parse(​'{ "expense_id": 42 }'​)
 => {"expense_id"=>42}

At this point, we don’t really care what the specific ID is, just that the data has this general structure. RSpec’s matchers make it easy to express this idea. Add the following highlighted lines inside your spec, just after the HTTP response check:

Get Effective Testing with RSpec 3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.