spec/config_spec.rb in anyway_config-1.0.0 vs spec/config_spec.rb in anyway_config-1.1.0

- old
+ new

@@ -4,11 +4,11 @@ describe Anyway::Config do let(:conf) { CoolConfig.new } let(:test_conf) { Anyway::TestConfig.new } - describe "config with name" do + context "config with name" do before(:each) do ENV.delete_if { |var| var =~ /^(cool|anyway)_/i } end specify { expect(CoolConfig.config_name).to eq "cool" } @@ -24,10 +24,29 @@ expect(conf).to respond_to(:port) expect(conf).to respond_to(:host) expect(conf).to respond_to(:user) end + describe "#to_h" do + subject(:config) { CoolConfig.new } + + it "returns deeply frozen hash" do + hashed = config.to_h + + expect(hashed).to be_a(Hash) + expect(hashed).to be_frozen + expect(hashed[:user]).to be_frozen + end + + it "returns new hash every time" do + hashed = config.to_h + hashed2 = config.to_h + + expect(hashed).to be_eql(hashed2) + end + end + describe "load from files" do it "set defaults" do expect(conf.port).to eq 8080 end @@ -85,11 +104,11 @@ expect(conf.user[:name]).to eq 'john' end end end - describe "config for name" do + context "config for name" do before(:each) do ENV.delete_if { |var| var =~ /^myapp_/i } end it "load data by config name", :aggregate_failures do @@ -100,8 +119,50 @@ expect(data[:test]).to eq 1 expect(data[:name]).to eq 'my_app' if Rails.application.respond_to?(:secrets) expect(data[:secret]).to eq 'my_secret' end + end + end + + context "config without defaults" do + let(:conf) { SmallConfig.new } + + it "works" do + expect(conf.meta).to be_nil + expect(conf.data).to be_nil + end + end + + context "when name is missing" do + let(:config) do + Class.new(described_class) + end + + it "raises ArgumentError" do + expect { config.new }.to raise_error(ArgumentError) + end + end + + context "extending config" do + let(:config) do + Class.new(described_class) do + config_name 'testo' + attr_config :test, debug: false + end + end + + it "adds new params" do + old_config = config.new + + expect(old_config.debug).to eq false + expect(old_config.test).to be_nil + + config.attr_config new_param: 'a' + + new_config = config.new + expect(new_config.debug).to eq false + expect(new_config.test).to be_nil + expect(new_config.new_param).to eq 'a' end end end