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

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

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