require 'rails_helper' module ConfigManager describe Toggle, :redis => true do describe ".toggles" do context "when there are no toggles" do it "returns an empty array" do Toggle.toggles.should == [] end it "returns the an array of currently defined toggles" do Toggles::Definition.new('d').type = 'set' Toggles::Definition.new('d1').type = 'boolean' Toggle.toggles.should have(2).toggles end end end describe ".active?" do before do definition.type = 'set' definition.acceptable_values = %w(a b) end let(:definition) { Toggles::Definition.new('d1') } context "when the challenge is in the acceptable answer test" do it "returns true" do Toggle.active?('d1', 'a').should be_truthy end end context "when the challenge is not in the acceptable answer test" do it "returns false" do Toggle.active?('d1', 'c').should be_falsy end end context "when the toggle is undefined" do it "returns false" do Toggle.active?('d2', 'a').should be_falsy end end end describe ".create" do let(:definition) { Toggles::Definition.new('ut') } it "creates a new boolean toggle" do Toggle.create(:name => 'ut', :type => 'boolean', :formatted_acceptable_values => 'true') definition.name.should == 'ut' definition.type.should == 'boolean' definition.acceptable_values.should == true end it "creates a new set toggle" do Toggle.create(:name => 'ut', :type => 'set', :formatted_acceptable_values => 'a, b , d') definition.name.should == 'ut' definition.type.should == 'set' definition.acceptable_values.should == %w{a b d} end it "returns a toggle definition instance" do Toggle.create(:name => 'ut', :type => 'set', :formatted_acceptable_values => 'a, b , d'). should be_instance_of(Toggles::Definition) end end describe ".delete" do it "deletes the toggle" do Toggle.create(:name => 'ut', :type => 'boolean', :formatted_acceptable_values => 'true') Toggle.delete('ut') d = Toggles::Definition.new('ut') d.name.should == 'ut' d.type.should == nil d.acceptable_values.should == nil end end describe ".from_yaml" do context "when file exists" do it "creates toggles (set and boolean) defined in file" do Toggle.from_yaml(Rails.root.join('config/toggles.yml')) Toggle.toggles.should have(3).toggles end it "returns number of created toggles and 0 failed" do Toggle.from_yaml(Rails.root.join('config/toggles.yml')).should == [3, 0] end context "when file has one bad toggle" do it "skips the bad toggle" do Toggle.from_yaml(Rails.root.join('config/bad_toggles.yml')) Toggle.toggles.should have(2).toggles end it "returns number of created toggles and 1 failed" do Toggle.from_yaml(Rails.root.join('config/bad_toggles.yml')).should == [2, 1] end end end context "when files doesn't exist" do it "raises an ENOENT error" do expect { Toggle.from_yaml(Rails.root.join('config/noexist.yml')) }. to raise_error(Errno::ENOENT) end end end end end