Skip to content

Commit

Permalink
Spec attribute value set in shared example helper
Browse files Browse the repository at this point in the history
Refactor specs to test attribute value is set correctly in a shared
example helper.

Remove separate redundant examples that test attribute value is set.

Use shared context for setting a single attribute value.
  • Loading branch information
robmckinnon committed Jul 18, 2016
1 parent 1a08b47 commit e45bc45
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 84 deletions.
96 changes: 17 additions & 79 deletions spec/lib/morph_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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


Expand Down
26 changes: 21 additions & 5 deletions spec/morph_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,41 +64,57 @@ 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
each_attribute { |a| expect(instance_methods(klass)).to include(a.to_s) }
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
each_attribute { |a| expect(instance_methods(klass)).to include("#{a}=") }
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
each_attribute { |a| expect(morph_methods(klass)).to include(a.to_s) }
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
each_attribute { |a| expect(morph_methods(klass)).to include("#{a}=") }
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

0 comments on commit e45bc45

Please sign in to comment.