spec/riveter/attributes_spec.rb in riveter-0.3.0 vs spec/riveter/attributes_spec.rb in riveter-0.6.0

- old
+ new

@@ -82,20 +82,100 @@ instance.an_attribute_from?.should be_truthy } it { instance.should respond_to(:an_attribute_to?) } + it { instance.should respond_to(:an_attribute_present?) } + it { instance.an_attribute = nil instance.an_attribute_to?.should be_falsey } it { instance.an_attribute_to = Date.today instance.an_attribute_to?.should be_truthy } + + it { + instance.an_attribute_from = Date.today + instance.an_attribute_to = Date.today + instance.an_attribute_present?.should be_truthy + } + + it { + instance.an_attribute_from = nil + instance.an_attribute_to = Date.today + instance.an_attribute_present?.should be_falsey + } + + it { + instance.an_attribute_from = Date.today + instance.an_attribute_to = nil + instance.an_attribute_present?.should be_falsey + } + + it { + instance.an_attribute_from = Date.today + instance.an_attribute_to = Date.today + instance.an_attribute_present?.should be_truthy + } + end + + describe "#min" do + before do + subject.attr_date_range :an_attribute_value, :min => 1 + subject.attr_date_range :an_attribute_block, :min => lambda { self.some_method } + end + + let(:instance) { subject.new() } + + it { instance.should respond_to(:an_attribute_value_min) } + it { instance.should respond_to(:an_attribute_value_min=) } + + it { + instance.an_attribute_value_min.should eq(1) + } + + it { + instance.an_attribute_value_min = 2 + instance.an_attribute_value_min.should eq(2) + } + + it "invokes the lambda in the context of self" do + expect(instance).to receive(:some_method) + instance.an_attribute_block_min + end + end + + describe "#max" do + before do + subject.attr_date_range :an_attribute_value, :max => 1 + subject.attr_date_range :an_attribute_block, :max => lambda { self.some_method } + end + + let(:instance) { subject.new() } + + it { instance.should respond_to(:an_attribute_value_max) } + it { instance.should respond_to(:an_attribute_value_max=) } + + it { + instance.an_attribute_value_max.should eq(1) + } + + it { + instance.an_attribute_value_max = 2 + instance.an_attribute_value_max.should eq(2) + } + + it "invokes the lambda in the context of self" do + expect(instance).to receive(:some_method) + instance.an_attribute_block_max + end + end + end it_should_behave_like "an attribute", :time, Time.new(2010, 1, 12, 8, 4, 45) do let(:assigned_value) { '2010-01-12 08:04:12' } let(:expected_value) { Time.new(2010, 1, 12, 8, 4, 12) } @@ -123,16 +203,11 @@ before do subject.attr_enum :product_type, TestEnum, :required => true end let(:instance) { subject.new() } - it { instance.should ensure_inclusion_of(:product_type).in_array(TestEnum.values) } - - it { should respond_to(:product_type_enum)} - it { subject.product_type_enum.should eq(TestEnum) } - it { should respond_to(:product_types)} - it { subject.product_types.should eq(TestEnum.collection)} + it { instance.should validate_inclusion_of(:product_type).in_array(TestEnum.values) } end end it_should_behave_like "an attribute", :array, [1, 2, 3] do let(:assigned_value) { %w(5 6 7) } @@ -142,48 +217,27 @@ it_should_behave_like "an attribute", :hash, {:a => :b} do let(:assigned_value) { {:c => '1', :d => '2'} } let(:expected_value) { {:c => 1, :d => 2} } end - it_should_behave_like "an attribute", :model, TestModel.new(), TestModel do - let(:assigned_value) { TestModel.new() } + it_should_behave_like "an attribute", :object, Object.new() do + let(:assigned_value) { Object.new() } + end - before do - allow_any_instance_of(TestModel).to receive(:valid?) { true } - allow(TestModel).to receive(:find_by) - end + it_should_behave_like "an attribute", :class, TestModel, [TestModel, TestModelWithAttributeDefaultValues] do + let(:assigned_value) { "TestModelWithAttributeDefaultValues" } + let(:expected_value) { TestModelWithAttributeDefaultValues } describe "additional" do before do - subject.attr_model :product, TestModel, :required => true + subject.attr_class :an_attribute, [TestModel, TestModelWithAttributeDefaultValues], :required => true end + let(:instance) { subject.new() } - it { should respond_to(:product_model)} - it { subject.product_model.should eq(TestModel) } - - it { - allow(TestModel).to receive(:find_by).with(:id => 1) { assigned_value } - - instance = subject.new() - instance.product = 1 - instance.product.should eq(assigned_value) - } - - it { - allow(TestModel).to receive(:find_by).with(:id => 1) { assigned_value } - expect(assigned_value).to receive(:valid?) { true } - - instance = subject.new() - instance.product = 1 - instance.valid? - } + it { instance.should validate_inclusion_of(:an_attribute).in_array([TestModel, TestModelWithAttributeDefaultValues]) } end end - - it_should_behave_like "an attribute", :object, Object.new() do - let(:assigned_value) { Object.new() } - end end describe "instance" do subject { TestClassWithAttributes.new() } @@ -200,11 +254,10 @@ subject.time.should eq(Time.new(2010, 1, 12, 14, 56)) subject.boolean.should eq(true) subject.enum.should eq(TestEnum::Member1) subject.array.should eq([1, 2, 3]) subject.hash.should eq({:a => :b}) - subject.model.should eq(TestModel) subject.object.should eq('whatever') end end describe "#attributes" do @@ -223,11 +276,11 @@ 'time' => Time.new(2010, 1, 12, 14, 56), 'boolean' => true, 'enum' => TestEnum::Member1, 'array' => [1, 2, 3], 'hash' => {:a => :b}, - 'model' => TestModel, - 'object' => 'whatever' + 'object' => 'whatever', + 'with_lambda_default' => subject }) } end describe "#persisted?" do