O'Reilly logo

Effective Testing with RSpec 3 by Myron Marston, Ian Dees

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required