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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.