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 Behavior

The failing spec is expecting the Ledger class’s record method to return error information if we pass in an invalid expense (one with no payee defined). Add the following highlighted lines to the record definition in app/ledger.rb:

 module​ ExpenseTracker
  RecordResult = Struct.new(​:success?​, ​:expense_id​, ​:error_message​)
 
 class​ Ledger
 def​ record(expense)
»unless​ expense.key?(​'payee'​)
» message = ​'Invalid expense: `payee` is required'
»return​ RecordResult.new(​false​, ​nil​, message)
»end
 
  DB[​:expenses​].insert(expense)
  id = DB[​:expenses​].max(​:id​)
  RecordResult.new(​true​, id, ​nil​)
 end
 
 def​ expenses_on(date)
 end
 

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