require 'rails_helper' module ConfigManager module Toggles describe Definition, :redis => true do let(:definition) { Definition.new('ut') } describe "#exists?" do context "when name is blank" do let(:definition) { Definition.new(nil) } it "returns false" do definition.exists?.should be_falsy end context "after setting the type" do before { definition.type == 'set' } it "still return false" do definition.exists?.should be_falsy end end end context "when type is not set" do it "returns false" do definition.exists?.should be_falsy end end context "when type is set" do before { definition.type = 'set' } it "returns true" do definition.exists?.should be_truthy end end end describe "validness" do context "when name is blank" do let(:definition) { Definition.new(nil) } it "is false" do definition.should_not be_valid end context "after setting the type" do before { definition.type == 'set' } it "is still false" do definition.should_not be_valid end it "doesn't really set the type" do definition.type.should be_nil end end end context "when type is not set" do it "is false" do definition.should_not be_valid end end context "when type is set" do before { definition.type = 'set' } it "is true" do definition.should be_valid end end end describe "#type" do context "when not set" do it "returns nil" do definition.type.should be_nil end end context "when set" do before { definition.type = 'set' } it "returns the stored value" do definition.type.should == 'set' end it "updates the stored value to a new value" do definition.type.should == 'set' definition.type = 'boolean' definition.type.should == 'boolean' end context "when the type is not in the supported list" do it "raises an error" do expect { definition.type = 'blah' }.to raise_error end end end end describe "#acceptable_values" do context "when not set" do context" when type is not set" do it "returns nil" do definition.acceptable_values.should == nil end end context "when type is set" do before { definition.type = 'set' } it "returns an empty array" do definition.acceptable_values.should == [] end end end context "when set" do context "when type is set to 'set'" do before do definition.type = 'set' definition.acceptable_values = %w(a b) end it "returns the stored values in set format" do definition.acceptable_values.should == %w(a b) end it "updates the stored values to a new value" do definition.acceptable_values = %w(c d) definition.acceptable_values.should == %w(c d) end end context "when type is boolean" do before { definition.type = 'boolean' } it "converts the string to a bool and store it" do definition.acceptable_values = 'true' definition.acceptable_values.should == true end end end end describe "#delete" do it "deletes the stored toggle" do definition.type = 'set' definition.acceptable_values = %w(a b) key = definition.instance_variable_get(:@key) $redis.exists(key).should be_truthy definition.delete $redis.exists(key).should be_falsy end end end end end