Refactoring

Both test cases have identical expressions setting up the ledger test double. You can remove the duplication by moving these into a common before hook. Place the following code just inside the first context block:

 let​(​:expense​) { { ​'some'​ => ​'data'​ } }
 
 before​ ​do
 allow​(ledger).to receive(​:record​)
  .with(expense)
  .and_return(RecordResult.new(​true​, 417, ​nil​))
 end

Now, remove the setup code from both examples:

 it​ ​'returns the expense id'​ ​do
  post ​'/expenses'​, JSON.generate(expense)
 
  parsed = JSON.parse(last_response.body)
 expect​(parsed).to ​include​(​'expense_id'​ => 417) ...

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.