Testing the Invalid Case

So far, our integration spec checks only the “happy path” of saving a valid expense. Let’s add a second spec that tries an expense missing a payee:

 context​ ​'when the expense lacks a payee'​ ​do
 it​ ​'rejects the expense as invalid'​ ​do
  expense.delete(​'payee'​)
 
  result = ledger.record(expense)
 
 expect​(result).not_to be_success
 expect​(result.expense_id).to eq(​nil​)
 expect​(result.error_message).to ​include​(​'`payee` is required'​)
 
 expect​(DB[​:expenses​].count).to eq(0)
 end
 end

Here, the Ledger instance should return the correct failure status and message, and the database should have no expenses in it.

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.