FrozenCanuck/Lebowski — Issue #21: exec_in_context loops should ensure the application context always switches back after the loop is finished.
Author: pythonesque
Created: 2011-10-14 18:17:31+00:00
State: open

If some Rspec fails (or anything else for that matter), exec_in_context doesn't reset the application context properly, and large numbers of tests afterwards start to fail.

Fix is basically to wrap the code in ensure blocks.

# In Lebowski::Foundation::Support::ApplicationContextManager

def exec_in_context(app, app_name=nil, &block)
previous_app_context = @current_application_context.clone
switch_application_context_to app, app_name
begin
yield app
switch_application_context_to previous_app_context[:app], previous_app_context[:app_name]
ensure
switch_application_context_to previous_app_context[:app], previous_app_context[:app_name]
end
end

def exec_driver_in_context(app, &block)
previous_app_context = nil
if not has_current_application_context?(app)
previous_app_context = @current_application_context.clone
switch_application_context_to app
end

begin
yield @driver

if not previous_app_context.nil?
switch_application_context_to previous_app_context[:app], previous_app_context[:app_name]
end
ensure
if not previous_app_context.nil?
switch_application_context_to previous_app_context[:app], previous_app_context[:app_name]
end
end

There are more elegant ways of doing it probably.