Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec shared examples must not be shared

This took me by surprise…

Seems that you can’t use the same name for 2 rspec shared example groups anywhere within your project. If you do, then when you run rake spec (to run all the specs), then rspec complains that a shared example group was declared with a non-unique name.

This happens even when the calls to shared_examples_for are contained within a describe block (one would presume that should scope the examples).

At first I tried to work around this by changing the names of the example groups (not ideal, but I can live with it).

But this became more of a problem when I wanted to factor out the example group to a separate file so I could share it between multiple spec files.

The specs work okay when run in isolation, but when I run the suite, rspec complains.

`ensure_shared_example_group_name_not_taken': \
Shared example group 'a person' already exists (ArgumentError)

Surely this is a common problem.

Is there something I’m missing here?

like image 941
Lachlan Cotter Avatar asked Jul 26 '11 14:07

Lachlan Cotter


1 Answers

As of rspec 2.6, shared examples are global. You can declare them in an example group, but they are not scoped to that group.

like image 51
David Chelimsky Avatar answered Sep 22 '22 12:09

David Chelimsky