-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework RSpec module with enable/disable methods
- Loading branch information
Tom Johnson
committed
Oct 25, 2016
1 parent
560136e
commit 45b21ca
Showing
2 changed files
with
155 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,66 @@ | ||
RSpec.configure do |config| | ||
config.before(:suite) do | ||
ActiveFedora::Noid.configure do |noid_config| | ||
noid_config.minter_class = ActiveFedora::Noid::Minter::File | ||
module ActiveFedora::Noid | ||
## | ||
# Provides a test minter conforming to the `ActiveFedora::Noid::Minter` | ||
# interface for use in unit tests. The test minter is faster and avoids | ||
# unexpected interactions with cleanup code commonly runs in test suites | ||
# (e.g. database cleanup). | ||
# | ||
# Applications should reenable their production minter for integration tests | ||
# when appropriate | ||
# | ||
# @example general use | ||
# ActiveFedora::Noid::RSpec.disable_production_minter! | ||
# # some unit tests with the test minter | ||
# ActiveFedora::Noid::RSpec.enable_production_minter! | ||
# # some integration tests with the original minter | ||
# | ||
# @example using a custom test minter | ||
# ActiveFedora::Noid::RSpec.disable_production_minter!(test_minter: Minter) | ||
# | ||
# @example use when included in RSpec config | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
# require 'active_fedora/noid/rspec' | ||
# | ||
# RSpec.configure do |config| | ||
# config.include(ActiveFedora::Noid::RSpec) | ||
# end | ||
# | ||
# before(:suite) { disable_production_minter! } | ||
# after(:suite) { enable_production_minter! } | ||
# | ||
module RSpec | ||
DEFAULT_TEST_MINTER = ActiveFedora::Noid::Minter::File | ||
|
||
## | ||
# Replaces the configured production minter with a test minter. | ||
# | ||
# @param test_minter [Class] an ActiveFedora::Noid::Minter implementation | ||
# to use as a replacement minter | ||
# @return [void] | ||
This comment has been minimized.
Sorry, something went wrong.
mjgiarlo
Member
|
||
def disable_production_minter!(test_minter: DEFAULT_TEST_MINTER) | ||
return nil if @original_minter | ||
This comment has been minimized.
Sorry, something went wrong.
mjgiarlo
Member
|
||
|
||
@original_minter = ActiveFedora::Noid.config.minter_class | ||
|
||
ActiveFedora::Noid.configure do |noid_config| | ||
noid_config.minter_class = test_minter | ||
end | ||
|
||
true | ||
end | ||
|
||
## | ||
# Re-enables the original configured minter. | ||
# | ||
# @return [void] | ||
def enable_production_minter! | ||
return nil unless @original_minter | ||
|
||
ActiveFedora::Noid.configure do |noid_config| | ||
noid_config.minter_class = @original_minter | ||
end | ||
|
||
@original_minter = nil | ||
true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
require 'active_fedora/noid/rspec' | ||
|
||
describe ActiveFedora::Noid::RSpec do | ||
include described_class | ||
|
||
let(:configured_minter) { @configured_minter } | ||
|
||
before do | ||
@configured_minter = Class.new(ActiveFedora::Noid::Minter::Base) | ||
@reset_minter = ActiveFedora::Noid.config.minter_class | ||
|
||
ActiveFedora::Noid.configure do |noid_config| | ||
noid_config.minter_class = @configured_minter | ||
end | ||
end | ||
|
||
after do | ||
ActiveFedora::Noid.configure do |noid_config| | ||
noid_config.minter_class = @reset_minter | ||
end | ||
end | ||
|
||
describe '#disable_production_minter!' do | ||
it 'changes the configured minter' do | ||
expect { disable_production_minter! } | ||
.to change { ActiveFedora::Noid.config.minter_class } | ||
.from(configured_minter) | ||
.to described_class::DEFAULT_TEST_MINTER | ||
end | ||
|
||
it 'accepts custom test minter at call time' do | ||
my_minter = Class.new(ActiveFedora::Noid::Minter::Base) | ||
|
||
expect { disable_production_minter!(test_minter: my_minter) } | ||
.to change { ActiveFedora::Noid.config.minter_class } | ||
.from(configured_minter) | ||
.to my_minter | ||
end | ||
|
||
it 'does not overwrite stored minter on second call' do | ||
var_name = :@original_minter | ||
|
||
disable_production_minter! | ||
|
||
expect { disable_production_minter! } | ||
.not_to change { described_class.instance_variable_get(var_name) } | ||
|
||
expect { enable_production_minter! } | ||
.to change { ActiveFedora::Noid.config.minter_class } | ||
.from(described_class::DEFAULT_TEST_MINTER) | ||
.to configured_minter | ||
end | ||
|
||
it 'disables after reenable' do | ||
disable_production_minter! | ||
enable_production_minter! | ||
|
||
expect { disable_production_minter! } | ||
.to change { ActiveFedora::Noid.config.minter_class } | ||
.from(configured_minter) | ||
.to described_class::DEFAULT_TEST_MINTER | ||
end | ||
end | ||
|
||
describe '#enable_production_minter!' do | ||
it 'does nothing when already enabled' do | ||
expect { enable_production_minter! } | ||
.not_to change { ActiveFedora::Noid.config.minter_class } | ||
end | ||
|
||
context 'with minter disabled' do | ||
before { disable_production_minter! } | ||
|
||
it 'reenables the originally configured minter' do | ||
expect { enable_production_minter! } | ||
.to change { ActiveFedora::Noid.config.minter_class } | ||
.from(described_class::DEFAULT_TEST_MINTER) | ||
.to configured_minter | ||
end | ||
|
||
it 'enables after re-disable' do | ||
enable_production_minter! | ||
disable_production_minter! | ||
|
||
expect { enable_production_minter! } | ||
.to change { ActiveFedora::Noid.config.minter_class } | ||
.from(described_class::DEFAULT_TEST_MINTER) | ||
.to configured_minter | ||
end | ||
end | ||
end | ||
end |
LGTM. Pls also include relevant changes to the README.