spec/toy/attributes_spec.rb in toystore-0.10.1 vs spec/toy/attributes_spec.rb in toystore-0.10.2
- old
+ new
@@ -154,10 +154,32 @@
end
it "ignores keys that are not attributes and do not have accessors defined" do
lambda { User.new(:taco => 'bell') }.should_not raise_error
end
+
+ it "uses accessor over writing attribute" do
+ User.attribute :age, Integer
+
+ User.class_eval do
+ def age=(value)
+ write_attribute :age, value + 10
+ end
+ end
+
+ record = User.new
+ record.attributes = {:age => 15}
+ record.age.should be(25)
+ end
+
+ it "uses write_attribute if accessor not present" do
+ User.attribute :age, Integer
+ record = User.new
+ record.should_receive(:respond_to?).with("age=") { false }
+ record.attributes = {:age => 10}
+ record.age.should be(10)
+ end
end
describe "reading an attribute" do
before do
User.attribute(:info, Hash)
@@ -167,10 +189,46 @@
it "returns the same instance" do
@user.info.should equal(@user.info)
end
end
+ describe "writing an attribute" do
+ before do
+ User.attribute :name, String
+
+ User.class_eval do
+ def alternate_name=(value)
+ write_attribute :name, value
+ end
+ end
+ end
+
+ it "assigns attribute value" do
+ user = User.new
+ user.alternate_name = 'Joe'
+ user.name.should eq('Joe')
+ end
+
+ context "when attribute not defined" do
+ before do
+ User.class_eval do
+ def pirate=(value)
+ write_attribute :pirate, value
+ end
+ end
+
+ @user = User.new
+ end
+
+ it "raises error" do
+ expect {
+ @user.pirate = 'arrrrrr'
+ }.to raise_error(Toy::AttributeNotDefined, "User does not have attribute pirate")
+ end
+ end
+ end
+
describe "declaring an attribute" do
before do
User.attribute :name, String
User.attribute :age, Integer
end
@@ -269,27 +327,6 @@
it "initializes to empty array" do
User.new.skills.should == []
end
end
-
- # https://github.com/newtoy/toystore/issues/13
- describe "Overriding initialize and setting an attribute before calling super" do
- before do
- User.attribute(:name, String)
- User.class_eval do
- def initialize(*)
- self.name = 'John'
- super
- end
- end
- end
-
- it "does not throw error" do
- lambda { User.new }.should_not raise_error
- end
-
- it "sets value" do
- User.new.name.should == 'John'
- end
- end
-end
\ No newline at end of file
+end