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 Failure

Now that your specs are testing the “happy path” of a successful expense, let’s turn our attention to the failure case. You can apply the hard-won knowledge from the previous sections, and start with an already-factored before hook. Here, the setup code will fill the RecordResult value object with a false success status and an error message:

 context​ ​'when the expense fails validation'​ ​do
 let​(​:expense​) { { ​'some'​ => ​'data'​ } }
 
 before​ ​do
 allow​(ledger).to receive(​:record​)
  .with(expense)
  .and_return(RecordResult.new(​false​, 417, ​'Expense incomplete'​))
 end
 
 it​ ​'returns an error message'​ ​do
  post ​'/expenses'​, JSON.generate(expense) ...

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