Determining the rspec context in which a helper method is run
Created by: odinhb
Subject of the issue
I'm writing a small helper function for my test suite, and I'd like to protect both future me and my colleagues from using said method inside rspec's before(:all)
hook (or extensions that run in the same context like let_it_be/before_all
from test-prof). It just doesn't make sense to run it in that way.
The current way of detecting this context relies on the string output of self.inspect
when running said method.
Alternatively, you can check the @__inspect_output
instance variable.
The check looks like this:
# this lives in a module, included in the example group
def my_helper
# self is an example group here
if inspect.include?("before(:context)")
raise friendly_error
end
# do stuff
end
You can see test-prof relying on the same hack here, and rspec-rails relying on it here.
Could we get an official way of detecting this context? It's easy to imagine this string changing w/o warning.
Your environment
- Ruby version: ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux]
- rspec-core version: 3.10.0