Rspec Shared Contexts Have to Be Included Separately (Sometimes)

Let’s say you have two shared_context blocks in your rspec spec that each redefines the same variable or mocks the same method:

You would expect that including them directly in a parent context would work:

Well it doesn’t. Due to how rspec (~3.5) is implemented, the two mocks of connected? method overwrite each other, making one of them fail. To avoid that, you have to put each shared_context block in a wrapping context:

 

Leave a Reply

Your email address will not be published. Required fields are marked *