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

Handling Success

To pass this spec, the /expenses route of our API needs to do three things:

  • Parse an expense from the request body

  • Use its Ledger (either a real database-based one or a fake one for testing) to record the expense

  • Return a JSON document containing the resulting expense ID

Change your /expenses route inside app/api.rb to the following code:

 post ​'/expenses'​ ​do
  expense = JSON.parse(request.body.read)
  result = @ledger.record(expense)
  JSON.generate(​'expense_id'​ => result.expense_id)
 end

Now, rerun the specs:

 $ ​​bundle exec rspec spec/unit/app/api_spec.rb
  truncated
 
 Finished in 0.02645 seconds (files took 0.14491 seconds to load)
 4 examples, 0 failures, 3 pending ...

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