Sharing Common Logic

Over the past several chapters, you’ve used several different techniques to share common setup logic.

The main three organization tools are let definitions, hooks, and helper methods. Here’s a snippet that contains all three of these items side by side. It’s a stripped-down version of the API specs you wrote for Chapter 5, Testing in Isolation: Unit Specs, including a minor refactoring from the exercises.

 RSpec.describe ​'POST a successful expense'​ ​do
 # let definitions
 let​(​:ledger​) { instance_double(​'ExpenseTracker::Ledger'​) }
 let​(​:expense​) { { ​'some'​ => ​'data'​ } }
 
 # hook
 before​ ​do
 allow​(ledger).to receive(​:record ...

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.