From 8e9c48b58094c5f55a510d5cbdf0328902bde209 Mon Sep 17 00:00:00 2001 From: IWASE Date: Wed, 8 Jan 2020 15:18:59 +0900 Subject: [PATCH] Add AC::TestSession#dig method like AD::Request::Session --- actionpack/lib/action_controller/test_case.rb | 5 +++++ actionpack/test/dispatch/session/test_session_test.rb | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb index f7eced7bba..d5c1aaca12 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/test_case.rb @@ -202,6 +202,11 @@ def destroy clear end + def dig(*keys) + keys = keys.map.with_index { |key, i| i.zero? ? key.to_s : key } + @data.dig(*keys) + end + def fetch(key, *args, &block) @data.fetch(key.to_s, *args, &block) end diff --git a/actionpack/test/dispatch/session/test_session_test.rb b/actionpack/test/dispatch/session/test_session_test.rb index e90162a5fe..2d36990250 100644 --- a/actionpack/test/dispatch/session/test_session_test.rb +++ b/actionpack/test/dispatch/session/test_session_test.rb @@ -43,6 +43,12 @@ def test_keys_and_values assert_equal %w(1 2), session.values end + def test_dig + session = ActionController::TestSession.new(one: { two: { three: "3" } }) + assert_equal("3", session.dig(:one, :two, :three)) + assert_nil(session.dig(:ruby, :on, :rails)) + end + def test_fetch_returns_default session = ActionController::TestSession.new(one: "1") assert_equal("2", session.fetch(:two, "2")) -- GitLab