Sharing Example Groups

As we’ve seen, plain old Ruby modules work really nicely for sharing helper methods across example groups. But that’s all they can share. If you want to reuse an example, a let construct or a hook, you’ll need to reach for another tool: shared example groups.

Just like its non-shared counterpart, a shared example group can contain examples, helper methods, let declarations, and hooks. The only difference is the way they’re used. A shared example group exists only to be shared.

In order to help you “get the words right,” RSpec provides multiple ways to create and use shared example groups. These come in pairs, with one method for defining a shared group and another for using it:

  • shared_context and include_context are for ...

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.