I have a module that can be configured like so:
module MyModule
mattr_accessor :setting
@@setting = :some_default_value
end
MyModule.setting = :custom_value
I'm testing different configuration options with RSpec, and found that the settings persist between different tests because they are class variables.
What's the best way to reload and re-initialize the module in between RSpec tests?
I came to this solution:
describe MyModule do
before :each do
# Removes the MyModule from object-space (the condition might not be needed):
Object.send(:remove_const, :MyModule) if Module.const_defined?(:MyModule)
# Reloads the module (require might also work):
load 'path/to/my_module.rb'
end
it "should have X value" do
MyModule.setting = :X
expect(MyModule.setting).to eq :X
end
end
Source: http://geminstallthat.wordpress.com/2008/08/11/reloading-classes-in-rspec/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With