From affe3e2e159028dc041ccad8f353823481c353da Mon Sep 17 00:00:00 2001 From: Sam Bostock Date: Wed, 28 Feb 2018 18:18:37 -0500 Subject: [PATCH] Bubble up IntegrationTest assertions count `IntegrationTest#open_session` uses `dup` to create the new session, which meant it had its own copy of `@assertions`. This prevented the assertions from being correctly counted and reported. Child sessions now have their `attr_accessor` overriden to delegate to the root session. --- actionpack/CHANGELOG.md | 12 ++++++++++++ .../lib/action_dispatch/testing/integration.rb | 10 ++++++++++ actionpack/test/controller/integration_test.rb | 8 ++++++++ 3 files changed, 30 insertions(+) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 9fcff6a6ca..d4ff4a5062 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,3 +1,15 @@ +* Include child session assertion count in ActionDispatch::IntegrationTest + + `IntegrationTest#open_session` uses `dup` to create the new session, which + meant it had its own copy of `@assertions`. This prevented the assertions + from being correctly counted and reported. + + Child sessions now have their `attr_accessor` overriden to delegate to the + root session. + + Fixes #32142 + + *Sam Bostock* Please check [6-0-stable](https://github.com/rails/rails/blob/6-0-stable/actionpack/CHANGELOG.md) for previous changes. diff --git a/actionpack/lib/action_dispatch/testing/integration.rb b/actionpack/lib/action_dispatch/testing/integration.rb index bb8b43ad4d..d30638195a 100644 --- a/actionpack/lib/action_dispatch/testing/integration.rb +++ b/actionpack/lib/action_dispatch/testing/integration.rb @@ -310,6 +310,7 @@ module ActionDispatch APP_SESSIONS = {} attr_reader :app + attr_accessor :root_session # :nodoc: def initialize(*args, &blk) super(*args, &blk) @@ -373,10 +374,19 @@ module ActionDispatch def open_session dup.tap do |session| session.reset! + session.root_session = self.root_session || self yield session if block_given? end end + def assertions # :nodoc: + root_session ? root_session.assertions : super + end + + def assertions=(assertions) # :nodoc: + root_session ? root_session.assertions = assertions : super + end + # Copy the instance variables from the current session instance into the # test instance. def copy_session_variables! #:nodoc: diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb index 4dddd98f9f..9cd9af43cc 100644 --- a/actionpack/test/controller/integration_test.rb +++ b/actionpack/test/controller/integration_test.rb @@ -138,6 +138,14 @@ class IntegrationTestTest < ActiveSupport::TestCase assert_not session1.equal?(session2) end + def test_child_session_assertions_bubble_up_to_root + assertions_before = @test.assertions + @test.open_session.assert(true) + assertions_after = @test.assertions + + assert_equal 1, assertions_after - assertions_before + end + # RSpec mixes Matchers (which has a #method_missing) into # IntegrationTest's superclass. Make sure IntegrationTest does not # try to delegate these methods to the session object.