From dd2709679ba1049991875300ab8b471901ec288c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Diego=20Piske?= Date: Fri, 6 Dec 2024 17:51:21 +0000 Subject: [PATCH] improve testing and allow disabling irontrail for a while --- lib/iron_trail/testing/rspec.rb | 29 +++++++++++++++++++----- spec/testing_itself.rb | 39 ++++++++++++++++++++++++++------- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/lib/iron_trail/testing/rspec.rb b/lib/iron_trail/testing/rspec.rb index 15f6eee..a124897 100644 --- a/lib/iron_trail/testing/rspec.rb +++ b/lib/iron_trail/testing/rspec.rb @@ -28,17 +28,34 @@ def disable! ActiveRecord::Base.connection.execute(sql) @enabled = false end + + def with_iron_trail(want_enabled:, &block) + was_enabled = IronTrail::Testing.enabled + + if want_enabled + ::IronTrail::Testing.enable! unless was_enabled + else + ::IronTrail::Testing.disable! if was_enabled + end + + block.call + ensure + if want_enabled && !was_enabled + ::IronTrail::Testing.disable! + elsif !want_enabled && was_enabled + ::IronTrail::Testing.enable! + end + end end end end RSpec.configure do |config| config.around(:each, iron_trail: true) do |example| - enabled = IronTrail::Testing.enabled - IronTrail::Testing.enable! unless enabled - - example.run - ensure - IronTrail::Testing.disable! unless enabled + IronTrail::Testing.with_iron_trail(want_enabled: true) { example.run } + end + config.around(:each, iron_trail: false) do |example| + raise "Using iron_trail: false does not do what you might think it does. To disable iron_trail, " \ + "use IronTrail::Testing.with_iron_trail(want_enabled: false) { ... } instead." end end diff --git a/spec/testing_itself.rb b/spec/testing_itself.rb index 4151ad3..b72d27c 100644 --- a/spec/testing_itself.rb +++ b/spec/testing_itself.rb @@ -12,19 +12,42 @@ person.update!(first_name: 'Jane') end - context 'with IronTrail disabled' do - it 'does not track anything' do - do_some_changes! - expect(person.reload.iron_trails.length).to be(0) + describe 'IronTrail::Testing#with_iron_trail' do + context 'when IronTrail is disabled but we enable it for a while' do + it 'tracks only while enabled' do + person.update!(first_name: 'Jim') + + expect(person.reload.iron_trails.length).to be(0) + + IronTrail::Testing.with_iron_trail(want_enabled: true) do + person.update!(first_name: 'Jane') + end + + expect(person.reload.iron_trails.length).to be(1) + + person.update!(first_name: 'Joe') + + expect(person.reload.iron_trails.length).to be(1) + end end end - context 'with IronTrail enabled through the helper', iron_trail: true do - it 'does not track anything' do - do_some_changes! + describe 'rspec helpers' do + context 'with IronTrail disabled' do + it 'does not track anything' do + do_some_changes! + + expect(person.reload.iron_trails.length).to be(0) + end + end + + context 'with IronTrail enabled through the helper', iron_trail: true do + it 'does not track anything' do + do_some_changes! - expect(person.reload.iron_trails.length).to be(3) + expect(person.reload.iron_trails.length).to be(3) + end end end end