Test conditions set by round_action in Rails
In a Rails application, the current locale is set to ApplicationController
via a callback around_action
. This is a cleaner solution than using it alone before_action
, which would leave the request-dependent local language behind .
class ApplicationController < ActionController::Base
around_action :with_locale
def with_locale
I18n.with_locale(find_current_locale) { yield }
end
end
Since the current locale is reset after the request completes, it's not easy to access the specific language of the request in a test. The before_filter
following test will pass with the help :
it 'sets locale from request'
get :action, locale: locale
I18n.locale.should == locale
end
I can't think of a way to implement this test to work with around_filter
without injecting some additional logic into the controller. Is there an easier way with RSpec?
source to share