diff --git a/spec/lib/morph_spec.rb b/spec/lib/morph_spec.rb index 3856456..57f23a0 100644 --- a/spec/lib/morph_spec.rb +++ b/spec/lib/morph_spec.rb @@ -29,27 +29,21 @@ def morphed_class let(:extended_morph) { extended_morphed_class.new } describe "when writer method that didn't exist before is called with non-nil value" do - before(:all) { initialize_morph } - after(:all) { unload_morph_class } - - let(:quack) { 'quack' } - let(:attribute) { 'noise' } - let(:expected_morph_methods_count) { 2 } + include_context 'single attribute value set', 'noise', 'quack' context do before do - remove_morph_methods - @morph.noise = quack + @morph.noise = value end it_should_behave_like "class with generated accessor methods added" it 'should return assigned value when reader method called' do - expect(@morph.noise).to eq quack + expect(@morph.noise).to eq value end it 'should return hash of attributes when morph_attributes called' do - expect(@morph.morph_attributes).to eq({attribute.to_sym => quack}) + expect(@morph.morph_attributes).to eq({attribute.to_sym => value}) end it 'should generate rails model generator script line, with given model name' do @@ -79,15 +73,9 @@ def self.extended_class end describe "when writer method that didn't exist before is called with nil value" do - after(:all) { unload_morph_class } + include_context 'single attribute value set', 'noise', nil - let(:attributes) { ['noise'] } - let(:expected_morph_methods_count) { 2 } - - before(:all) do - initialize_morph - @morph.noise= nil - end + before { @morph.noise = value } it_should_behave_like "class with generated accessor methods added" end @@ -187,37 +175,19 @@ def self.extended_class end describe "when writer method that didn't exist before is called with blank space attribute value" do - after(:all) { unload_morph_class } - - let(:attributes) { [:noise] } - let(:expected_morph_methods_count) { 2 } + include_context 'single attribute value set', 'noise', ' ' - before(:all) do - initialize_morph - @morph.noise = ' ' - end + before { @morph.noise = value } it_should_behave_like "class with generated accessor methods added" end describe 'when morph method used to set attribute value' do - before(:all) { initialize_morph } - after(:all) { unload_morph_class } - - let(:attribute) { 'reading' } - let(:value) { '20 Mar 2008' } - let(:expected_morph_methods_count) { 2 } + include_context 'single attribute value set', 'reading', '20 Mar 2008' - before do - remove_morph_methods - @morph.morph('Reading', value) - end + before { @morph.morph('Reading', value) } it_should_behave_like "class with generated accessor methods added" - - it 'should return assigned value when reader method called' do - expect(@morph.reading).to eq value - end end describe 'when morph method used to set an attribute value hash' do @@ -226,6 +196,7 @@ def self.extended_class let(:expected_morph_methods_count) { 6 } let(:attributes) { [:drink,:sugars,:milk] } + let(:value) { 'tea' } before do remove_morph_methods @@ -268,10 +239,6 @@ def self.extended_class end it_should_behave_like "class with generated accessor methods added" - - it 'should return assigned value when reader method called' do - @morph.send(@attribute.to_sym) == @age - end end describe "when morph method used to set japanese and latin unicode attribute name with a value" do before :all do initialize_morph; end @@ -291,37 +258,21 @@ def self.extended_class end it_should_behave_like "class with generated accessor methods added" - - it 'should return assigned value when reader method called' do - @morph.send(@attribute.to_sym) == @age - end end =end describe 'when morph method used to set blank space attribute value' do - after(:all) { unload_morph_class } + include_context 'single attribute value set', 'pizza', ' ' - let(:attributes) { [:pizza] } - let(:expected_morph_methods_count) { 2 } - - before(:all) do - initialize_morph - @morph.morph('Pizza', ' ') - end + before(:each) { @morph.morph('Pizza', value) } it_should_behave_like "class with generated accessor methods added" end describe 'when morph method used to set nil attribute value' do - after(:all) { unload_morph_class } + include_context 'single attribute value set', 'pizza', nil - let(:attributes) { [:pizza] } - let(:expected_morph_methods_count) { 2 } - - before(:all) do - initialize_morph - @morph.morph('Pizza', nil) - end + before { @morph.morph('Pizza', value) } it_should_behave_like "class with generated accessor methods added" end @@ -344,24 +295,11 @@ def self.extended_class end describe "when writer method called matches a class reader method" do + include_context 'single attribute value set', 'name', 'Morph' - before(:all) { initialize_morph } - after(:all) { unload_morph_class } - - let(:attribute) { 'name' } - let(:value) { 'Morph' } - let(:expected_morph_methods_count) { 2 } - - before do - remove_morph_methods - @morph.name = value - end + before { @morph.name = value } it_should_behave_like "class with generated accessor methods added" - - it 'should return assigned value when reader method called' do - expect(@morph.name).to eq value - end end diff --git a/spec/morph_spec_helper.rb b/spec/morph_spec_helper.rb index e8d1075..22d8ead 100644 --- a/spec/morph_spec_helper.rb +++ b/spec/morph_spec_helper.rb @@ -64,7 +64,13 @@ def each_attribute klass = morphed_class unless klass end - it 'should add reader method to class instance_methods list' do + it 'sets first attribute value correctly' do + attribute = nil + each_attribute {|a| attribute = a unless attribute} + expect(@morph.send(attribute)).to eq value + end + + it 'adds reader method to class instance_methods list' do if RUBY_VERSION >= "1.9" each_attribute { |a| expect(instance_methods(klass)).to include(a.to_s.to_sym) } else @@ -72,7 +78,7 @@ def each_attribute end end - it 'should add writer method to class instance_methods list' do + it 'adds writer method to class instance_methods list' do if RUBY_VERSION >= "1.9" each_attribute { |a| expect(instance_methods(klass)).to include("#{a}=".to_sym) } else @@ -80,7 +86,7 @@ def each_attribute end end - it 'should add reader method to class morph_methods list' do + it 'adds reader method to class morph_methods list' do if RUBY_VERSION >= "1.9" each_attribute { |a| expect(morph_methods(klass)).to include(a.to_s.to_sym) } else @@ -88,7 +94,7 @@ def each_attribute end end - it 'should add writer method to class morph_methods list' do + it 'adds writer method to class morph_methods list' do if RUBY_VERSION >= "1.9" each_attribute { |a| expect(morph_methods(klass)).to include("#{a}=".to_sym) } else @@ -96,9 +102,19 @@ def each_attribute end end - it 'should only have generated accessor methods in morph_methods list' do + it 'only has generated accessor methods in morph_methods list' do expect(morph_methods(klass).size).to eq expected_morph_methods_count end end +shared_context 'single attribute value set' do |field, value| + before(:all) { initialize_morph } + after(:all) { unload_morph_class } + + let(:attribute) { field } + let(:expected_morph_methods_count) { 2 } + let(:value) { value } + + before { remove_morph_methods } +end