I am trying to recursively declare contexts in Rspec from within a Helpers module.
(My code would be using these contexts in an unusual way, namely to recursively make assertions about keys in a nested Hash. Maybe I could solve this problem in a different way, but that's beside the point.)
A minimal complete example would be:
module Helpers
def context_bar
context "bar" do
it "baz" do
expect(true).to be true
end
end
end
end
include Helpers
describe "foo" do
Helpers.context_bar
end
If I now execute this code in Rspec it fails with:
RuntimeError:
Creating an isolated context from within a context is not allowed. Change `RSpec.context` to `context` or move this to a top-level scope.
I can then refactor it as this:
def context_bar
context "bar" do
it "baz" do
expect(true).to be true
end
end
end
describe "foo" do
context_bar
end
And that works just fine for me, although I lose the benefit of the readability that comes with having this method and similar methods inside a module name space.
Is there any way for me to make this work?
(Note that there is a superficial similarity of this question to others like this one, or in the Rspec docs here. This seems to make Helpers available inside examples, but it won't allow me to actually declare a context.)