spec/hashme/properties_spec.rb in hashme-0.2.1 vs spec/hashme/properties_spec.rb in hashme-0.2.2

- old
+ new

@@ -8,102 +8,111 @@ property :age, Fixnum end Kernel.const_set("Aux", @aux_model) end - before :each do - @model = Class.new do + let :model do + Class.new do include Hashme property :name, String end - - @obj = @model.new end + let :obj do + model.new + end describe "#get_attribute" do it "should provide object in model" do - @obj[:key1] = 'value' - expect(@obj.get_attribute(:key1)).to eql('value') + obj[:key1] = 'value' + expect(obj.get_attribute(:key1)).to eql('value') end end describe "#set_attribute" do - it "should be posible to set attribute not defined as property" do - @obj.set_attribute('key1', 'value1') - @obj.set_attribute(:key2, 'value2') - expect(@obj[:key1]).to eql('value1') - expect(@obj[:key2]).to eql('value2') + it "should NOT be posible to set attribute not defined as property" do + obj.set_attribute('key1', 'value1') + obj.set_attribute(:key2, 'value2') + expect(obj[:key1]).to be_nil + expect(obj[:key2]).to be_nil end it "should set and cast attribute with property" do - property = @model.send(:properties)[:name] + property = model.send(:properties)[:name] name = "Fred Flinstone" - expect(property).to receive(:build).with(@obj, name).and_return(name) - @obj.set_attribute(:name, name) - expect(@obj[:name]).to eql(name) + expect(property).to receive(:build).with(obj, name).and_return(name) + obj.set_attribute(:name, name) + expect(obj[:name]).to eql(name) end end + describe "#attribtues=" do + it "should set attributes on the model" do + expect(obj.name).to be_nil + obj.attributes = { name: 'Sam' } + expect(obj.name).to eql("Sam") + end + end + describe ".properties" do it "should be instantiated after property set" do - expect(@model.properties).to_not be_nil - expect(@model.properties.class).to eql(Hash) + expect(model.properties).to_not be_nil + expect(model.properties.class).to eql(Hash) end it "should be empty if no properties" do - model = Class.new do + mod = Class.new do include Hashme end - expect(model.properties).to be_empty + expect(mod.properties).to be_empty end it "should be inherited from parent models" do - mod = Class.new(@model) do + mod = Class.new(model) do property :surname, String end expect(mod.properties.keys).to include(:name) expect(mod.properties.keys).to include(:surname) # Make sure we don't update the parent! - expect(@model.properties.keys).to_not include(:surname) + expect(model.properties.keys).to_not include(:surname) end end describe ".property" do it "should fail if no type is defined" do - expect(@model.properties.length).to eql(1) + expect(model.properties.length).to eql(1) expect { - @model.property :foobar + model.property :foobar }.to raise_error(ArgumentError) - expect(@model.properties.length).to eql(1) + expect(model.properties.length).to eql(1) end it "should create a new property with helper methods" do - expect(@model.properties.length).to eql(1) - @model.property :desc, String - expect(@model.properties.length).to eql(2) + expect(model.properties.length).to eql(1) + model.property :desc, String + expect(model.properties.length).to eql(2) - prop = @model.properties[:desc] + prop = model.properties[:desc] expect(prop.class).to eql(Hashme::Property) - expect(@obj).to respond_to(:desc) - expect(@obj).to respond_to(:desc=) + expect(obj).to respond_to(:desc) + expect(obj).to respond_to(:desc=) - @obj.desc = "test" - expect(@obj.desc).to eql("test") + obj.desc = "test" + expect(obj.desc).to eql("test") end it "should return nil on property with no default" do - @model.property :nickname, String - expect(@obj.nickname).to be_nil + model.property :nickname, String + expect(obj.nickname).to be_nil end it "should create helper method with support for default values" do - @model.property :name, String, :default => "Sam" - expect(@obj.name).to eql("Sam") + model.property :name, String, :default => "Sam" + expect(obj.name).to eql("Sam") end end end