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?

+3


source to share


1 answer


How about checking what I18n.with_locale

was called with the correct parameters.



it 'sets locale from request'
  allow(I18n).to receive(:with_locale)
  get :action, locale: locale
  expect(I18n).to have_received(:with_locale).with(locale)
end

      

+2


source







All Articles