spec/gorillib/builder_spec.rb in gorillib-0.4.1pre vs spec/gorillib/builder_spec.rb in gorillib-0.4.2pre
- old
+ new
@@ -1,29 +1,15 @@
require 'spec_helper'
+require 'support/model_test_helpers'
+require 'gorillib/hash/compact'
# libs under test
require 'gorillib/builder'
-require 'gorillib/builder/field'
require 'gorillib/collection/model_collection'
-# testing helpers
-require 'gorillib/hash/compact'
-require 'model_test_helpers'
-
describe Gorillib::Builder, :model_spec => true, :builder_spec => true do
- let(:smurf_class) do
- class Gorillib::Test::Smurf
- include Gorillib::Builder
- magic :smurfiness, Integer
- magic :weapon, Symbol
- end
- Gorillib::Test::Smurf
- end
- let(:poppa_smurf ){ smurf_class.receive(:name => 'Poppa Smurf', :smurfiness => 9, :weapon => 'staff') }
- let(:smurfette ){ smurf_class.receive(:name => 'Smurfette', :smurfiness => 11, :weapon => 'charm') }
-
#
# IT BEHAVES LIKE A MODEL
# (maybe you wouldn't notice if it was just one little line)
#
it_behaves_like 'a model'
@@ -65,10 +51,18 @@
it 'with a block of arity 1, calls the block passing self' do
expect_7 = nil ; expect_obj = nil
wildcat.receive!({}){|c| expect_7 = 7 ; expect_obj = c }
expect_7.should == 7 ; expect_obj.should == wildcat
end
+ it 'with a block, returns its return value' do
+ val = mock_val
+ wildcat.receive!{ val }.should == val
+ wildcat.receive!{|obj| val }.should == val
+ end
+ it 'with no block, returns nil' do
+ wildcat.receive!.should be_nil
+ end
end
context ".magic" do
let(:subject_class){ car_class }
context do
@@ -127,18 +121,20 @@
end
it "does not create a writer method #foo=" do
wildcat.should respond_to(:doors)
wildcat.should_not respond_to(:doors=)
end
+
+
end
context 'collections' do
subject{ garage }
- let(:sample_val){ Gorillib::ModelCollection.receive([wildcat], :name, car_class) }
+ let(:sample_val){ Gorillib::ModelCollection.receive([wildcat], key_method: :name, item_type: car_class) }
let(:raw_val ){ [ wildcat.attributes ] }
it_behaves_like "a model field", :cars
- it("#read_attribute is an empty collection if never set"){ subject.read_attribute(:cars).should == Gorillib::ModelCollection.new }
+ it("#read_attribute is an empty collection if never set"){ subject.read_attribute(:cars).should == Gorillib::ModelCollection.new(key_method: :to_key) }
it 'a collection holds named objects' do
garage.cars.should be_empty
# create a car with a hash of attributes
@@ -155,16 +151,16 @@
garage.car(:ford_39).should ==(ford_39)
garage.car(:ford_39).should_not equal(ford_39)
# examine the whole collection
garage.cars.keys.should == [:cadzilla, :wildcat, :ford_39]
- garage.cars.should == Gorillib::ModelCollection.receive([cadzilla, wildcat, ford_39], :name, car_class)
+ garage.cars.should == Gorillib::ModelCollection.receive([cadzilla, wildcat, ford_39], key_method: :name, item_type: car_class)
end
it 'lazily autovivifies collection items' do
garage.cars.should be_empty
garage.car(:chimera).should be_a(car_class)
- garage.cars.should == Gorillib::ModelCollection.receive([{:name => :chimera}], :name, car_class)
+ garage.cars.should == Gorillib::ModelCollection.receive([{:name => :chimera}], key_method: :name, item_type: car_class)
end
context 'collection getset method' do
it 'clxn(:name, existing_object) -- replaces with given object, does not call block' do
test = nil