spec/lib/attributes_spec.rb in api_resource-0.4.3 vs spec/lib/attributes_spec.rb in api_resource-0.5.0

- old
+ new

@@ -1,15 +1,43 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') include ApiResource describe "Attributes" do + + before(:all) do + TestResource.reload_class_attributes + end after(:all) do TestResource.reload_class_attributes end - + + context "setters" do + + it "should allow setting of protected attributes individually" do + test_resource = TestResource.new + test_resource.protected_attr = 100 + test_resource.protected_attr.should eql(100) + end + + it "should not allow mass assignment of protected attributes" do + test_resource = TestResource.new + lambda{ + test_resource.attributes = {:protected_attr => 100} + }.should raise_error + end + + it "should not allow mass assignment in the constructor" do + lambda{ + TestResource.new({:protected_attr => 100}) + }.should raise_error + end + + end + + context "Defining, getting, and setting attributes" do it "should be able to define known attributes" do TestResource.define_attributes :attr1, :attr2 TestResource.attribute?(:attr1).should be_true TestResource.attribute?(:attr2).should be_true @@ -74,16 +102,16 @@ tst.attr1 = "test" tst.attr1.should eql("test") end it "should create protected attributes for unknown attributes trying to be loaded" do - tst = TestResource.new({:attr1 => "attr1", :attr3 => "attr3"}) + + TestResource.connection.stubs(:get => {:attr1 => "attr1", :attr3 => "attr3"}) + tst = TestResource.find(1) + tst.attr3?.should be_true tst.attr3.should eql("attr3") - lambda { - tst.attr3 = "test" - }.should raise_error end end it "should define methods for testing for reading and writing known attributes" do TestResource.define_attributes :attr1, :attr2 @@ -112,19 +140,9 @@ tst.attr1.should eql "abc" tst.attr2.should eql "test" tst.attr3.should eql "123" end - end - - context "Protected attributes" do - it "should allow protected attributes that cannot be changed" do - TestResource.define_protected_attributes :pattr3 - lambda { - tst = TestResource.new - tst.pattr3 = "test" - }.should raise_error - end end context "Dirty tracking" do context "Changes to attributes" do it "should implement dirty tracking for attributes" do \ No newline at end of file