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) ...

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.