Last active
April 25, 2024 16:43
-
-
Save petitviolet/1f7965e2c3624f475282d71df6b36c58 to your computer and use it in GitHub Desktop.
Revisions
-
petitviolet revised this gist
Nov 10, 2020 . 1 changed file with 10 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -32,22 +32,30 @@ class ItemsController < ActionController::Base 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" -
petitviolet created this gist
Nov 9, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,76 @@ # 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] res = AnotherController.dispatch(:get, request, response) render json: { msg: "OK", body: JSON.parse(res[2].body) }, status: 200 end end class AnotherController < ActionController::Base include Rails.application.routes.url_helpers def get item = { "1" => "hello", "2" => "world", }[params[:item_id]] render json: { item: item }, status: 200 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