require File.expand_path('spec/spec_helper') require './lib/yaml_properties' describe YamlProperties do context "in general" do before do File.stub(:open) YamlProperties.dev_environment = false Psych.stub(:load).and_return properties end let(:properties) do { "life_the_universe_and_everything" => 42, "some_string_value" => "something", "a boolean" => true, "parent" => { "child"=>"Egg"} } end context "in non dev" do before do YamlProperties.dev_environment = false YamlProperties.reset! end specify "it caches yaml file" do YamlProperties.should_receive(:load_properties). exactly(1).times.and_return properties YamlProperties.some_string_value YamlProperties.some_string_value end end context "in dev environment" do before do YamlProperties.dev_environment = true YamlProperties.reset! end specify "reload so Rails/Sinatra don't need restarting" do YamlProperties.should_receive(:load_properties).at_least(2).times.and_return properties YamlProperties.some_string_value YamlProperties.some_string_value end end specify "overriding attributes (in e.g. tests)" do YamlProperties.override_attribute :life_the_universe_and_everything, 13 YamlProperties.life_the_universe_and_everything.should == 13 YamlProperties.reset! YamlProperties.life_the_universe_and_everything.should == 42 end specify "non-existent attribute overrides shouldn't work" do ->{ YamlProperties.override_attribute :unset_attribute, "let's not allow typos to cause us grief"}. should raise_error ArgumentError end specify do YamlProperties.properties.should == properties end specify do YamlProperties.life_the_universe_and_everything.should == 42 end specify do YamlProperties.some_string_value.should == "something" end specify do YamlProperties.send("a boolean").should == true end specify do YamlProperties.parent.should == {"child" => "Egg"} end context "extending a module" do module Acme extend YamlProperties end specify do Acme.life_the_universe_and_everything.should == 42 end end end context "overriding yaml_file default" do module Mystery extend YamlProperties def self.yaml_file "./spec/fixtures/test.yml" end end specify do Mystery.life_the_universe_and_everything.should == "Nobody knows" end end context "with erb" do module ErbMystery extend YamlProperties def self.yaml_file "./spec/fixtures/erb.yml" end end before { ENV['mistery_message'] = "Everybody knows" } after { ENV.delete 'mistery_message' } specify do ErbMystery.life_the_universe_and_everything.should == "Everybody knows" end end end