How to set locale default_url_options for functional tests (Rails)

后端 未结 10 1736
予麋鹿
予麋鹿 2020-12-30 07:00

In my application_controller, I have the following set to include the locale with all paths generated by url_for:

  def default_url_options(options={})
    {         


        
10条回答
  •  执笔经年
    2020-12-30 07:19

    alias_method_chain is deprecated in Rails 5, and it seems the Behavior process method has changed.

    Here's my modification of Martin Carel's answer above, adapted to Rails 5.

    RSpec.configure do |config|
      module ActionController
        class TestCase
          module Behavior
            module LocaleParameter
              def process(action, parameters = {params: {}})
                unless I18n.locale.nil?
                  parameters[:params][:locale] = I18n.locale
                end
    
                super(action, parameters)
              end
            end
    
            prepend Behavior::LocaleParameter
    
          end
        end
      end
    end
    

    I'm by no means an expert in Rails or Ruby, so if something can be improved in this answer, let me know and I'll change it.

提交回复
热议问题