Last Updated: February 25, 2016
·
623
· perldork

Padrino - rspec - adding dynamic code to all controllers during testing

We add in code that allows us to send mock parameters to controller actions via Capybara tests for all controllers while testing -so we can simulate session state (.e.g. user being logged in ).

In Rails you do this by re-opening ApplicationController in spec/spechelper.rb and adding in a beforefilter.

In Padrino you can do this by adding custom code to app.rb in a before block - the before block is called for every controller action.

configure :test do
   before do
     params.keys.each do |param|
       if param =~ /^mock_/
         mock_param = param.gsub(/mock_/, '') 
         session[ mock_param ] = params[ param ]
         logger.debug %{ #{mock_param} set to #{params[ param ]}} 
       end 
     end 
   end 
 end