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

Querying the Data

Saving expenses is all fine and good, but it’d be nice to retrieve them. We want to allow users to fetch expenses by date, so let’s post a few expenses with different dates and then request the expenses for one of those dates. We expect the app to respond with just the expenses recorded on that date.

Posting one expense after another will get really old if we have to keep repeating all that code. Let’s extract that helper logic into a post_expense helper method inside the RSpec.describe block:

 def​ post_expense(expense)
  post ​'/expenses'​, JSON.generate(expense)
 expect​(last_response.status).to eq(200)
 
  parsed = JSON.parse(last_response.body) ...

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