Run rspec "before" before starting rails initializers

I would like to run the rspec before block to set some elements before starting the Rails initializers, so I can check what the initializer should do. Is it possible?

+4
source share
1 answer

If the logic in your initializers is complex enough, you should test it, you should extract it to a helper, which you can isolate and test without being in the context of the initializer.

complex_initializer.rb

 config.database.foo = calculate_hard_stuff() config.database.bar = other_stuff() 

You can extract this into the checked helper (lib / config / database.rb)

 module Config::DatabaseHelper def self.generate_config {:foo => calculate_hard_stuff, :bar => other_stuff) end def calculate_hard_stuff # Hard stuff here end end 

... then just plug in the configuration data in your initializer

 db_config_values = Config::DatabaseHelper.generate_config config.database.foo = db_config_values[:foo] config.database.bar = db_config_values[:bar] 

... and check the complex definition / calculation of the configuration in a separate test, where you can highlight the inputs.

 describe Config::DatabaseHelper do describe '.calculate_hard_stuff' do SystemValue.stubs(:config => value) Config::DatabaseHelper.calculate_hard_stuff.should == expected_value end end 
+2
source

All Articles