spec/attributes_spec.rb in mongo_doc-0.3.1 vs spec/attributes_spec.rb in mongo_doc-0.3.2
- old
+ new
@@ -1,6 +1,6 @@
-require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+require 'spec_helper'
describe "MongoDoc::Attributes" do
class AttributesTest
include MongoDoc::Attributes
end
@@ -10,37 +10,93 @@
AttributesTest.new.should respond_to(:_id=)
end
context ".key" do
class TestKeys
- include MongoDoc::Document
+ include MongoDoc::Attributes
+
+ key :attr1, :attr2
end
it "adds its arguments to _keys" do
- TestKeys.key :attr1, :attr2
TestKeys._keys.should == [:attr1, :attr2]
end
describe "accessors" do
- before do
- TestKeys.key :attr1
- end
-
subject do
TestKeys.new
end
+
it "has an attr1 reader" do
should respond_to(:attr1)
end
it "has an attr1 writer" do
should respond_to(:attr1=)
end
end
+ context "default values" do
+ class TestDefault
+ include MongoDoc::Attributes
+
+ key :with_default, :default => 'value'
+ end
+
+ let(:object) { TestDefault.new }
+
+ it "uses the default value" do
+ object.with_default.should == 'value'
+ end
+
+ it "only uses the default value once" do
+ object.with_default.should == 'value'
+ class << object
+ def _default_with_default
+ 'other value'
+ end
+ end
+ object.with_default.should == 'value'
+ end
+
+ it "does not set the default value if the setter is invoked first" do
+ object.with_default = nil
+ object.with_default.should be_nil
+ end
+ end
+
+ context "specified type" do
+ class TestType
+ include MongoDoc::Attributes
+
+ key :birthdate, :type => Date
+ end
+
+ let(:object) { TestType.new }
+
+ it "does not call Type.cast_from_string when the set value is not a string" do
+ Date.should_not_receive :cast_from_string
+ object.birthdate = Date.today
+ end
+
+ context "when the accessor is set with a string" do
+ let(:date) { Date.today }
+
+ it "delegates to Type.cast_from_string to set the value" do
+ Date.should_receive(:cast_from_string).with(date.to_s)
+ object.birthdate = date.to_s
+ end
+
+ it "sets the value to the result of the case" do
+ object.birthdate = date.to_s
+ object.birthdate.should == date
+ end
+ end
+ end
+
describe "used with inheritance" do
class TestParent
- include MongoDoc::Document
+ include MongoDoc::Attributes
key :parent_attr
end
class TestChild < TestParent