spec/gorillib/model_spec.rb in gorillib-0.4.1pre vs spec/gorillib/model_spec.rb in gorillib-0.4.2pre

- old
+ new

@@ -1,22 +1,11 @@ require 'spec_helper' -require 'model_test_helpers' +require 'support/model_test_helpers' require 'gorillib/model' -describe Gorillib::Model, :model_spec => true do - let(:smurf_class) do - class Gorillib::Test::Smurf - include Gorillib::Model - field :smurfiness, Integer - field :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') } - +describe Gorillib::Model, :model_spec do let(:simple_model) do class Gorillib::Test::SimpleModel include Gorillib::Model field :my_field, :whatever field :str_field, String @@ -51,12 +40,12 @@ :my_field => 'accepted as-is', :str_field => :bob, :sym_field => 'converted_to_sym' }) obj.attributes.should == { :my_field => 'accepted as-is', :str_field => 'bob', :sym_field=>:converted_to_sym } end it 'handles nested structures' do - deep_obj = nested_model.receive(:str_field => 'deep, man', :smurf => poppa_smurf.attributes) - deep_obj.attributes.should == { :str_field => 'deep, man', :smurf => poppa_smurf, :sym_field=>nil, :my_field => nil, } + deep_obj = nested_model.receive(:str_field => 'deep, man', :smurf => papa_smurf.attributes) + deep_obj.attributes.should == { :str_field => 'deep, man', :smurf => papa_smurf, :sym_field=>nil, :my_field => nil, } end end context ".field" do it "describes an attribute" do @@ -79,13 +68,13 @@ end it "supplies a writer method #foo= to call write_attribute(:foo)" do example_inst.should_receive(:write_attribute).with(:my_field, mock_val) (example_inst.my_field = mock_val).should == mock_val end - it "supplies a receiver method #receive_foo to call write_attribute(:foo) and return self" do - example_inst.should_receive(:write_attribute).with(:my_field, mock_val) - (example_inst.receive_my_field(mock_val)).should == example_inst + it "supplies #receive_foo, which does write_attribute(:foo) and returns the new value " do + example_inst.should_receive(:write_attribute).with(:my_field, mock_val).and_return('okey doke!') + (example_inst.receive_my_field(mock_val)).should == 'okey doke!' end it "sets visibility of reader with :reader => ()" do described_class.field :test_field, Integer, :reader => :private, :writer => false described_class.public_instance_methods.should_not include(:test_field) described_class.private_instance_methods.should include(:test_field) @@ -169,10 +158,13 @@ it "goes throught the #write_attribute interface" do example_inst.should_receive(:write_attribute).with(:my_field, 7) example_inst.should_receive(:write_attribute).with(:str_field, 'yo') example_inst.receive! 'my_field'=>7, :str_field=>'yo' end + it "returns nil with no block given" do + example_inst.receive!('my_field'=>7, :str_field=>'yo').should be_nil + end end context '#== -- two models are equal if' do let(:subklass){ Class.new(described_class) } let(:obj_2){ described_class.receive(:my_field => 69) } @@ -225,9 +217,18 @@ context '.typename' do it 'has a typename that matches its underscored class name' do described_class.typename.should == 'gorillib.test.simple_model' end + end + + context '.inspect' do + it('is pretty'){ smurf_class.inspect.should == 'Gorillib::Test::Smurf[name,smurfiness,weapon]' } + it('is pretty even if class is anonymous'){ Class.new(smurf_class).inspect.should == 'anon[name,smurfiness,weapon]' } + end + context '.inspect_compact' do + it('is just the class name'){ smurf_class.inspect_compact.should == "Gorillib::Test::Smurf" } + it('is detailed if class is anonymous'){ Class.new(smurf_class).inspect_compact.should == "anon[name,smurfiness,weapon]" } end describe Gorillib::Model::NamedSchema do context ".meta_module" do let(:basic_field_names){ [ :my_field, :my_field=, :receive_my_field, :receive_str_field, :receive_sym_field, :str_field, :str_field=, :sym_field, :sym_field= ]}