Skip to content

Commit

Permalink
improve testing and allow disabling irontrail for a while
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepiske committed Dec 6, 2024
1 parent 9316d1a commit dd27096
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
29 changes: 23 additions & 6 deletions lib/iron_trail/testing/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
39 changes: 31 additions & 8 deletions spec/testing_itself.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit dd27096

Please sign in to comment.