In Chapter 7, Structuring Code Examples, we discussed three ways to share code across many example groups:
You’ve used all of these techniques throughout this book. By default, they all share code unconditionally. If you define, say, a before hook in your RSpec.configure block, the hook will run for every example.
Often, though, you want to use a certain bit of shared code only for specific examples. For instance, in Isolating Your Specs Using Database Transactions, you defined an around hook to wrap a database transaction around only the examples tagged with :db: