Skip to content

Instantly share code, notes, and snippets.

@petitviolet
Last active November 10, 2020 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petitviolet/1f7965e2c3624f475282d71df6b36c58 to your computer and use it in GitHub Desktop.
Save petitviolet/1f7965e2c3624f475282d71df6b36c58 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", "6.0.0"
end
require "rack/test"
require "action_controller/railtie"
class TestApp < Rails::Application
config.root = __dir__
config.hosts << "example.org"
config.session_store :cookie_store, key: "cookie_store_key"
secrets.secret_key_base = "secret_key_base"
config.logger = Logger.new($stdout)
Rails.logger = config.logger
routes.draw do
get "/items/:id" => "items#show"
end
end
class ItemsController < ActionController::Base
include Rails.application.routes.url_helpers
def show
request.params[:item_id] = request.params[:id]
ac = AnotherController.new
res = ac.dispatch(:get, request, response)
item_id = ac.instance_variable_get(:@item_id)
puts "instance var item_id: #{item_id}"
render json: { msg: "OK", body: JSON.parse(res[2].body) }, status: 200
end
end
class AnotherController < ActionController::Base
include Rails.application.routes.url_helpers
before_action :set_item_id
def get
item = {
"1" => "hello",
"2" => "world",
}[@item_id]
render json: { item: item }, status: 200
end
private
def set_item_id
@item_id = params.require(:item_id)
end
end
require "minitest/autorun"
class BugTest < Minitest::Test
include Rack::Test::Methods
def test_returns_success
get "/items/1"
assert last_response.ok?
assert last_response.body == '{"msg":"OK","body":{"item":"hello"}}', last_response.body
get "/items/2"
assert last_response.ok?
assert last_response.body == '{"msg":"OK","body":{"item":"world"}}', last_response.body
get "/items/3"
assert last_response.ok?
assert last_response.body == '{"msg":"OK","body":{"item":null}}', last_response.body
end
private
def app
Rails.application
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment