application_handle_info_spec.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. require 'rails_helper'
  2. RSpec.describe ApplicationHandleInfo do
  3. describe '.use' do
  4. it 'requires a block' do
  5. expect { described_class.use('foo') }
  6. .to raise_error(ArgumentError)
  7. end
  8. context 'for a given starting ApplicationHandleInfo' do
  9. # This `around` block is identical to ApplicationHandleInfo.use.
  10. #
  11. # Q: So why don't we just use it here to DRY things up?
  12. # A: Because that's the method we're trying to test, dummy!
  13. #
  14. # Q: Why can't we do `before { ApplicationHandleInfo.current = 'foo' }` instead?
  15. # A: Because that would change `ApplicationHandleInfo.current` for all subsequent specs.
  16. # (RSpec uses database transactions to keep test environments clean,
  17. # but `ApplicationHandleInfo.current` lives outside of the database.)
  18. around do |example|
  19. original = described_class.current
  20. described_class.current = 'foo'
  21. example.run
  22. described_class.current = original
  23. end
  24. it 'runs the block using the given ApplicationHandleInfo' do
  25. described_class.use('bar') do
  26. expect(described_class.current).to eq('bar')
  27. end
  28. end
  29. it 'resets ApplicationHandleInfo to its original value' do
  30. described_class.use('bar') {}
  31. expect(described_class.current).to eq('foo')
  32. end
  33. context 'when an error is raised in the given block' do
  34. it 'does not rescue the error, and still resets ApplicationHandleInfo' do
  35. expect { described_class.use('bar') { raise } }
  36. .to raise_error(StandardError)
  37. .and not_change(described_class, :current)
  38. end
  39. end
  40. end
  41. end
  42. end