RSpec recipe: remove constants created during a test execution
Dmitry Shvetsov
61

We ran into this issue testing some modules in our codebase. One option that worked well for us was to create the modules/classes with instantiation syntax and avoid creating a constant:

greeter_klass = Class.new do
def initialize(object)
@object = object
end
  def hello
"Hello, my name is #{@object.name}"
end
end

This also works nicely with let as well, if you’re into that sort of thing:

let(:my_module) { Module.new { def foo; end } }
Like what you read? Give Robert Fletcher a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.