Replace :application_handle spec helper with ApplicationHandleInfo.use method

This commit is contained in:
Ryan Lue 2019-04-11 01:14:34 +08:00 committed by Thorsten Eckel
parent e6697cdd21
commit 545339fe8e
3 changed files with 45 additions and 5 deletions

View file

@ -12,4 +12,14 @@ module ApplicationHandleInfo
current.split('.')[1] == 'postmaster'
end
def self.use(name)
raise ArgumentError, 'requires a block' if !block_given?
orig = current
self.current = name
yield
ensure
self.current = orig
end
end

View file

@ -0,0 +1,34 @@
require 'rails_helper'
RSpec.describe ApplicationHandleInfo do
describe '.use' do
it 'requires a block' do
expect { ApplicationHandleInfo.use('foo') }
.to raise_error(ArgumentError)
end
context 'for a given starting ApplicationHandleInfo' do
before { ApplicationHandleInfo.current = 'foo' }
it 'runs the block using the given ApplicationHandleInfo' do
ApplicationHandleInfo.use('bar') do
expect(ApplicationHandleInfo.current).to eq('bar')
end
end
it 'resets ApplicationHandleInfo to its original value' do
ApplicationHandleInfo.use('bar') {}
expect(ApplicationHandleInfo.current).to eq('foo')
end
context 'when an error is raised in the given block' do
it 'does not rescue the error, and still resets ApplicationHandleInfo' do
expect { ApplicationHandleInfo.use('bar') { raise } }
.to raise_error(StandardError)
.and not_change { ApplicationHandleInfo.current }
end
end
end
end
end

View file

@ -1,11 +1,7 @@
RSpec.configure do |config|
config.around(:each, :application_handle) do |example|
ApplicationHandleInfo.current = example.metadata[:application_handle]
begin
ApplicationHandleInfo.use(example.metadata[:application_handle]) do
example.run
ensure
ApplicationHandleInfo.current = nil
end
end
end