File: testing_spec.rb

package info (click to toggle)
ruby-feature 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 184 kB
  • sloc: ruby: 760; sh: 20; makefile: 9
file content (107 lines) | stat: -rw-r--r-- 3,459 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
require 'spec_helper'
require 'feature/testing'

shared_examples 'a testable repository' do
  before do
    expect(Feature.active?(:active_feature)).to be_truthy
    expect(Feature.active?(:another_active_feature)).to be_truthy
    expect(Feature.active?(:deactive_feature)).to be_falsey
    expect(Feature.active?(:another_deactive_feature)).to be_falsey
  end

  after do
    expect(Feature.active?(:active_feature)).to be_truthy
    expect(Feature.active?(:another_active_feature)).to be_truthy
    expect(Feature.active?(:deactive_feature)).to be_falsey
    expect(Feature.active?(:another_deactive_feature)).to be_falsey
  end

  describe '.run_with_activated' do
    it 'activates a deactivated feature' do
      Feature.run_with_activated(:deactive_feature) do
        expect(Feature.active?(:deactive_feature)).to be_truthy
      end
    end

    it 'activates multiple deactivated features' do
      Feature.run_with_activated(:deactive_feature, :another_deactive_feature) do
        expect(Feature.active?(:deactive_feature)).to be_truthy
        expect(Feature.active?(:another_deactive_feature)).to be_truthy
      end
    end
  end

  describe '.run_with_deactivated' do
    it 'deactivates an activated feature' do
      Feature.run_with_deactivated(:active_feature) do
        expect(Feature.active?(:active_feature)).to be_falsey
      end
    end

    it 'deactivates multiple activated features' do
      Feature.run_with_deactivated(:active_feature, :another_active_feature) do
        expect(Feature.active?(:active_feature)).to be_falsey
        expect(Feature.active?(:another_active_feature)).to be_falsey
      end
    end
  end
end

describe 'Feature testing support' do
  context 'without auto_refresh' do
    before(:all) do
      repository = Feature::Repository::SimpleRepository.new
      repository.add_active_feature(:active_feature)
      repository.add_active_feature(:another_active_feature)
      Feature.set_repository(repository)
    end

    it_behaves_like 'a testable repository'
  end

  context 'with auto_refresh' do
    before(:all) do
      repository = Feature::Repository::SimpleRepository.new
      repository.add_active_feature(:active_feature)
      repository.add_active_feature(:another_active_feature)
      Feature.set_repository(repository, true)
    end

    it_behaves_like 'a testable repository'

    describe '.run_with_deactivated' do
      it 'should disable perform_initial_refresh for the first call to Feature.active?' do
        Feature.run_with_activated(:deactive_feature) do
          expect(Feature.active?(:deactive_feature)).to be_truthy
        end
      end
    end

    describe '.run_with_deactivated' do
      it 'should disable perform_initial_refresh for the first call to Feature.active?' do
        Feature.run_with_deactivated(:active_feature) do
          expect(Feature.active?(:active_feature)).to be_falsey
        end
      end
    end
  end

  context 'with no features activated' do
    before(:all) do
      repository = Feature::Repository::SimpleRepository.new
      Feature.set_repository(repository)
    end

    describe '.run_with_activated' do
      it 'should not raise an error' do
        expect { Feature.run_with_activated(:foo) {} }.to_not raise_error
      end
    end

    describe '.run_with_deactivated' do
      it 'should not raise an error' do
        expect { Feature.run_with_deactivated(:foo) {} }.to_not raise_error
      end
    end
  end
end