Skip to content

Instantly share code, notes, and snippets.

@khustochka
Created June 27, 2020 20:50
Show Gist options
  • Save khustochka/fa9f2d6b5f5d2ed3b9e240d24f85b7e3 to your computer and use it in GitHub Desktop.
Save khustochka/fa9f2d6b5f5d2ed3b9e240d24f85b7e3 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails"
end
require "action_controller/railtie"
class TestApp < Rails::Application
config.root = __dir__
config.hosts << "example.org"
secrets.secret_key_base = "secret_key_base"
config.logger = Logger.new($stdout)
Rails.logger = config.logger
routes.draw do
get "(:locale)/demo" => "test#demo"
end
end
class TestController < ActionController::Base
include Rails.application.routes.url_helpers
def demo
locale = request.path_parameters[:locale]
render plain: "locale=#{locale}"
end
end
require "minitest/autorun"
require 'rails/test_help'
class TestControllerTest < ActionController::TestCase
def test_html_returns_correct
get :demo, params: {locale: "en"}
Rails.logger.warn "\n>> HTML: request.path_parameters[\"locale\"] = #{request.path_parameters["locale"]}\n" +
">> HTML: request.path_parameters[:locale] = #{request.path_parameters[:locale]}\n"
assert_equal "locale=en", response.body
end
def test_json_returns_correct
get :demo, params: {locale: "en"}, format: :json
Rails.logger.warn "\n>> JSON: request.path_parameters[\"locale\"] = #{request.path_parameters["locale"]}\n" +
">> JSON: request.path_parameters[:locale] = #{request.path_parameters[:locale]}\n"
assert_equal "locale=en", response.body
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment