Created
November 18, 2025 17:17
-
-
Save nicolas-brousse/ceff7e0b810383843dbc908a6690c889 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| require "bundler/inline" | |
| gemfile(true) do | |
| source "https://rubygems.org" | |
| gem "rails", "~> 8.0.0" | |
| gem "http_accept_language" | |
| gem 'friendly_id' | |
| # If you want to test against edge Rails replace the previous line with this: | |
| # gem "rails", github: "rails/rails", branch: "main" | |
| gem "net-smtp", github: "ruby/net-smtp", ref: "d496a829f9b99adb44ecc1768c4d005e5f7b779e", require: false | |
| end | |
| require "action_controller/railtie" | |
| require "action_mailer/railtie" | |
| require "minitest/autorun" | |
| require "rack/test" | |
| puts "Rails version: #{Rails::VERSION::STRING}" | |
| class TestApp < Rails::Application | |
| config.load_defaults Rails::VERSION::STRING.to_f | |
| config.root = __dir__ | |
| config.eager_load = false | |
| config.hosts << "example.org" | |
| config.secret_key_base = "secret_key_base" | |
| config.logger = Logger.new($stdout) | |
| config.i18n.default_locale = :en | |
| config.i18n.available_locales = %i[en fr] | |
| config.i18n.fallbacks = [I18n.default_locale] | |
| config.action_mailer.default_url_options = { host: "example.org" } | |
| end | |
| Rails.application.initialize! | |
| Rails.application.routes.draw do | |
| scope "/(:locale)", locale: /en|fr/ do | |
| root to: "test#index" | |
| get "/index", to: "test#index" | |
| resources :articles, only: %i[index show], path: "gazette" | |
| end | |
| end | |
| class ApplicationController < ActionController::Base | |
| before_action :set_locale | |
| include Rails.application.routes.url_helpers | |
| def set_locale | |
| I18n.locale = params[:locale] || I18n.default_locale | |
| end | |
| def preferred_locale | |
| http_accept_language.compatible_language_from(I18n.available_locales) \ | |
| || I18n.default_locale | |
| end | |
| def default_url_options | |
| return {} if I18n.locale == I18n.default_locale | |
| { locale: I18n.locale } | |
| end | |
| end | |
| class TestController < ApplicationController | |
| def index | |
| render json: { locale: I18n.locale, default_url_options:, path: root_path, url: root_url, url_for: url_for({ controller: :test, action: :index }) } | |
| end | |
| end | |
| class ArticlesController < ApplicationController | |
| def index | |
| render json: { locale: I18n.locale, default_url_options:, path: articles_path, url: articles_url, url_for: url_for({ controller: :articles, action: :index }) } | |
| end | |
| def show | |
| render json: { locale: I18n.locale, default_url_options:, path: article_path("1"), url: article_url("1"), url_for: url_for({ controller: :articles, action: :show, id: 1}) } | |
| end | |
| end | |
| class TestMailer < ActionMailer::Base | |
| def hello_world | |
| mail from: "[email protected]", to: "[email protected]" do |format| | |
| format.html { render inline: "<p><%= root_url %></p>" } | |
| format.text { render inline: "<%= root_url %>" } | |
| end | |
| end | |
| end | |
| class ControllerTest < ActiveSupport::TestCase | |
| include Rack::Test::Methods | |
| def test_blank_returns_success | |
| get "/" | |
| assert last_response.ok? | |
| assert_pattern { JSON.parse(last_response.body, symbolize_names: true) => { locale: "en", path: "/", url: "http://example.org/", default_url_options: {}, url_for: "http://example.org/" } } | |
| end | |
| def test_fr_returns_success | |
| get "/fr/" | |
| assert last_response.ok? | |
| assert_pattern { JSON.parse(last_response.body, symbolize_names: true) => { locale: "fr", path: "/fr", url: "http://example.org/fr", default_url_options: { locale: "fr" }, url_for: "http://example.org/fr" } } | |
| end | |
| def articles_en_returns_success | |
| get "/gazette" | |
| assert last_response.ok? | |
| assert_pattern { JSON.parse(last_response.body, symbolize_names: true) => { locale: "en", path: "/gazette", url: "http://example.org/gazette", default_url_options: { locale: "en" }, url_for: "http://example.org/gazette" } } | |
| end | |
| def articles_fr_returns_success | |
| get "/fr/gazette" | |
| assert last_response.ok? | |
| assert_pattern { JSON.parse(last_response.body, symbolize_names: true) => { locale: "fr", path: "/fr/gazette", url: "http://example.org/fr/gazette", default_url_options: { locale: "fr" }, url_for: "http://example.org/fr/gazette" } } | |
| end | |
| def article_en_returns_success | |
| get "/gazette/1" | |
| assert last_response.ok? | |
| assert_pattern { JSON.parse(last_response.body, symbolize_names: true) => { locale: "en", path: "/gazette/1", url: "http://example.org/gazette/1", default_url_options: { locale: "en" }, url_for: "http://example.org/gazette/1" } } | |
| end | |
| def article_fr_returns_success | |
| get "/fr/gazette/1" | |
| assert last_response.ok? | |
| assert_pattern { JSON.parse(last_response.body, symbolize_names: true) => { locale: "fr", path: "/fr/gazette/1", url: "http://example.org/fr/gazette/1", default_url_options: { locale: "fr" }, url_for: "http://example.org/fr/gazette/1" } } | |
| end | |
| private | |
| def app | |
| Rails.application | |
| end | |
| end | |
| class MailerTest < ActionMailer::TestCase | |
| test "renders HTML and Text body" do | |
| email = TestMailer.hello_world | |
| email.deliver_now | |
| assert_dom_email do | |
| assert_dom "p", text: "http://example.org/" | |
| end | |
| assert_includes email.text_part.body, "http://example.org/" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment