Skip to content

Commit

Permalink
Fix flaky localization tests (#1664)
Browse files Browse the repository at this point in the history
Fixes #1663

We shouldn't replace the whole Rails' app's RouteSet, particularly when the `@routes`
instance variable already provides the hook.

Instead we can mirror what Rails does internally:
https://github.com/rails/rails/blob/a59ddc714fa0403cbd8c7ae6abdec2f71ee7b63f/actionpack/test/controller/test_case_test.rb#L1224C1-L1236C8
  • Loading branch information
kaspth authored Sep 12, 2024
1 parent 05c7c2a commit 1b19936
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions test/controllers/application/localization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@
class ApplicationControllerTest < ActionController::TestCase
include Devise::Test::ControllerHelpers

def setup
ApplicationController.class_eval do
def any_action
setup do
@controller = Class.new(ApplicationController) do
def locale
render plain: I18n.locale
end
end

# If disable_clear_and_finalize is set to true, Rails will not clear other
# routes when calling again the draw method. Look at the source code at:
# https://www.rubydoc.info/gems/actionpack/ActionDispatch/Routing/RouteSet#draw-instance_method
Rails.application.routes.disable_clear_and_finalize = true
end.new

Rails.application.routes.draw do
get "any_action" => "application#any_action"
@routes = ActionDispatch::Routing::RouteSet.new
@routes.draw do
get "locale" => "anonymous#locale"
end

@actual_locales = I18n.available_locales
Expand All @@ -32,23 +28,23 @@ def teardown
test "team locale is nil, user locale is nil" do
sign_in @user

get :any_action
get :locale
assert_equal I18n.default_locale.to_s, response.body
end

test "team locale is nil, user locale is nil, HTTP_ACCEPT_LANGUAGE equals es" do
@request.headers["HTTP_ACCEPT_LANGUAGE"] = "es"
request.headers["HTTP_ACCEPT_LANGUAGE"] = "es"
sign_in @user

get :any_action
get :locale
assert_equal "es", response.body
end

test "team locale is es, user locale is nil" do
sign_in @user
@user.current_team.update!(locale: "es")

get :any_action
get :locale
assert_equal "es", response.body
end

Expand All @@ -57,15 +53,15 @@ def teardown
@user.current_team.update!(locale: "es")
@user.update!(locale: "de")

get :any_action
get :locale
assert_equal "de", response.body
end

test "team locale is nil, user locale is de" do
sign_in @user
@user.update!(locale: "de")

get :any_action
get :locale
assert_equal "de", response.body
end

Expand All @@ -74,26 +70,26 @@ def teardown
@user.update!(locale: "")
@user.current_team.update!(locale: "es")

get :any_action
get :locale
assert_equal "es", response.body
end

test "user not signed in" do
get :any_action
get :locale
assert_equal I18n.default_locale.to_s, response.body
end

test "user not signed in and browser sends HTTP_ACCEPT_LANGUAGE" do
@request.headers["HTTP_ACCEPT_LANGUAGE"] = "de"
request.headers["HTTP_ACCEPT_LANGUAGE"] = "de"

get :any_action
get :locale
assert_equal "de", response.body
end

test "user not signed in and browser sends HTTP_ACCEPT_LANGUAGE with unknown value" do
@request.headers["HTTP_ACCEPT_LANGUAGE"] = "this-language-does-not-really-exist"
request.headers["HTTP_ACCEPT_LANGUAGE"] = "this-language-does-not-really-exist"

get :any_action
get :locale
assert_equal I18n.default_locale.to_s, response.body
end
end

0 comments on commit 1b19936

Please sign in to comment.