Skip to content

Commit

Permalink
add some unit tests for new rake tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinnio committed May 11, 2019
1 parent 38c99a5 commit fd19b5a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/tasks/support/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ def status_label(enabled)
enabled.nil? ? '' : (enabled ? 'ON' : 'OFF')
end

def find_feature_by_name(name)
features.find { |f| f.name == name } || raise("Feature :#{name} is not defined.")
end

def find_strategy_by_name(name)
strategies.find { |s| s.name == name } || raise("Strategy :#{name} is not available.")
end

def table_header
%w[feature description] + strategies.map(&:name)
end
Expand Down Expand Up @@ -48,6 +40,16 @@ def clear_feature!(feature_name, strategy_name)

strategy.clear!(feature.key)
end

protected

def find_feature_by_name(name)
features.find { |f| f.name == name } || raise("Feature :#{name} is not defined.")
end

def find_strategy_by_name(name)
strategies.find { |s| s.name == name } || raise("Strategy :#{name} is not available.")
end
end
end
end
57 changes: 57 additions & 0 deletions test/unit/tasks/support/methods_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require File.expand_path('../../../../test_helper', __FILE__)
require File.expand_path('../../../../../lib/tasks/support/methods', __FILE__)

describe Flipflop::Rake::SupportMethods do
subject do
object = Object.new
object.extend Flipflop::Rake::SupportMethods
end

describe '#status_label' do
it 'returns the "enabled" label when its argument is "true"' do
assert_equal 'ON', subject.status_label(true)
end

it 'returns the "disabled" label when its argument is "false"' do
assert_equal 'OFF', subject.status_label(false)
end

it 'returns the "unset" label when its argument is "nil"' do
assert_equal '', subject.status_label(nil)
end
end

describe '#switch_feature!' do
def with_feature_and_strategy
# Stubs finder methods to avoid going into FeatureSet code.
feature = Flipflop::FeatureDefinition.new(:world_domination)
subject.stub :find_feature_by_name, feature do
strategy = Flipflop::Strategies::TestStrategy.new(name: 'test')
subject.stub :find_strategy_by_name, strategy do
yield(strategy, feature) if block_given?
end
end
end

it 'enables a feature using a strategy' do
with_feature_and_strategy do |strategy, feature|
subject.switch_feature! 'world_domination', 'test', true
assert_equal true, strategy.enabled?(feature.key)
end
end

it 'disables a feature using a strategy' do
with_feature_and_strategy do |strategy, feature|
subject.switch_feature! 'world_domination', 'test', false
assert_equal false, strategy.enabled?(feature.key)
end
end

it 'clears a feature using a strategy' do
with_feature_and_strategy do |strategy, feature|
subject.clear_feature! 'world_domination', 'test'
assert_nil strategy.enabled?(feature.key)
end
end
end
end

0 comments on commit fd19b5a

Please sign in to comment.