Skip to content

Commit

Permalink
Rework RSpec module with enable/disable methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Johnson committed Oct 25, 2016
1 parent 560136e commit 45b21ca
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 4 deletions.
67 changes: 63 additions & 4 deletions lib/active_fedora/noid/rspec.rb
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.

Copy link
@mjgiarlo

mjgiarlo Oct 25, 2016

Member

LGTM. Pls also include relevant changes to the README.

This comment has been minimized.

# 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.

Copy link
@mjgiarlo

mjgiarlo Oct 25, 2016

Member

I'm not sure if there's better behavior here, but the new methods do go out of their way to return boolean-ish values. I'm just as OK with the return values as they are now as they would be to:

  1. Remove the explicit nils, trues, and falses and leave # @return [void]
  2. Change the YARD to # @return [Boolean] (and return false instead of nil in the new methods' guard clauses)

/shrug

def disable_production_minter!(test_minter: DEFAULT_TEST_MINTER)
return nil if @original_minter

This comment has been minimized.

Copy link
@mjgiarlo

mjgiarlo Oct 25, 2016

Member

Perhaps hide the ivar behind a new method, like def original_minter_set??, and call the new method in both existing methods.


@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
92 changes: 92 additions & 0 deletions spec/unit/rspec_spec.rb
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

0 comments on commit 45b21ca

Please sign in to comment.