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

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.

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