spec/unit/chozo/config/json_spec.rb in chozo-0.1.0 vs spec/unit/chozo/config/json_spec.rb in chozo-0.2.0

- old
+ new

@@ -1,20 +1,8 @@ require 'spec_helper' -require 'shared_examples/chozo/config' -TestJSONConfig = Class.new do - include Chozo::Config::JSON - - attribute :name - validates_presence_of :name - - attribute :job -end - describe Chozo::Config::JSON do - it_behaves_like "Chozo::Config", TestJSONConfig - let(:json) do %( { "name": "reset", "job": "programmer", @@ -22,40 +10,39 @@ } ) end describe "ClassMethods" do - subject { TestJSONConfig } + subject do + Class.new(Chozo::Config::JSON) do + attribute :name, required: true + attribute :job + end + end describe "::from_json" do - it "returns an instance of the including class" do + it "returns an instance of the inheriting class" do subject.from_json(json).should be_a(subject) end it "assigns values for each defined attribute" do config = subject.from_json(json) config[:name].should eql("reset") config[:job].should eql("programmer") end - - it "does not set an attribute value for undefined attributes" do - config = subject.from_json(json) - - config[:status].should be_nil - end end describe "::from_file" do let(:file) { tmp_path.join("test_config.json").to_s } before(:each) do File.open(file, "w") { |f| f.write(json) } end - it "returns an instance of the including class" do - subject.from_file(file).should be_a(TestJSONConfig) + it "returns an instance of the inheriting class" do + subject.from_file(file).should be_a(subject) end it "sets the object's filepath to the path of the loaded file" do subject.from_file(file).path.should eql(file) end @@ -68,11 +55,16 @@ end end end end - subject { TestJSONConfig.new } + subject do + Class.new(Chozo::Config::JSON) do + attribute :name, required: true + attribute :job + end.new + end describe "#to_json" do before(:each) do subject.name = "reset" subject.job = "programmer" @@ -88,22 +80,26 @@ end end describe "#from_json" do it "returns an instance of the updated class" do - subject.from_json(json).should be_a(subject.class) + subject.from_json(json).should be_a(Chozo::Config::JSON) end it "assigns values for each defined attribute" do config = subject.from_json(json) - config[:name].should eql("reset") - config[:job].should eql("programmer") + config.name.should eql("reset") + config.job.should eql("programmer") end + end - it "does not set an attribute value for undefined attributes" do - config = subject.from_json(json) + describe "#save" do + it "raises a ConfigSaveError if no path is set or given" do + subject.path = nil - config[:status].should be_nil + lambda { + subject.save + }.should raise_error(Chozo::Errors::ConfigSaveError) end end end