Last Updated: February 25, 2016
·
232
· kriansa

Clearing RSpec hooks inside a spec

There aren't a lot of use cases where you want to clear the global RSpec before/after hooks, but I needed it in a specific file from my suite that I didn't want to just copy the spec_helper.rb to do that.

RSpec.configuration.hooks.send(:instance_variable_get, :@data)[:before][:example] = RSpec::Core::Hooks::HookCollection.new

RSpec.configuration.hooks.send(:instance_variable_get, :@data)[:after][:example] = RSpec::Core::Hooks::HookCollection.new

That's more a workaround than a real solution. Just to remember: that must be done before the describe block.