From fdac9062c1d2ba9d42084c160b5c9ca1f34ece4b Mon Sep 17 00:00:00 2001 From: Rafael Reggiani Manzo Date: Fri, 22 May 2015 08:22:34 -0300 Subject: [PATCH] ReadingGroup#destroy with attributes memoization clean up and covered by unit tests --- app/models/reading_group.rb | 1 + features/step_definitions/reading_group_steps.rb | 1 + spec/models/reading_group_spec.rb | 29 +++++++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/app/models/reading_group.rb b/app/models/reading_group.rb index 633609b..683ad41 100644 --- a/app/models/reading_group.rb +++ b/app/models/reading_group.rb @@ -25,6 +25,7 @@ class ReadingGroup < KalibroClient::Entities::Configurations::ReadingGroup def destroy attributes.destroy unless attributes.nil? + @attributes = nil super end end diff --git a/features/step_definitions/reading_group_steps.rb b/features/step_definitions/reading_group_steps.rb index 5914b4e..a49cdfc 100644 --- a/features/step_definitions/reading_group_steps.rb +++ b/features/step_definitions/reading_group_steps.rb @@ -61,6 +61,7 @@ Then(/^I should be in the Edit Reading Group page$/) do end Then(/^the Sample Reading Group should not be there$/) do + expects(@reading_group.attributes).to be_nil expect { ReadingGroup.find(@reading_group.id) }.to raise_error end diff --git a/spec/models/reading_group_spec.rb b/spec/models/reading_group_spec.rb index 5b3640b..584fd4c 100644 --- a/spec/models/reading_group_spec.rb +++ b/spec/models/reading_group_spec.rb @@ -45,6 +45,33 @@ describe ReadingGroup, :type => :model do expect(subject.readings).to include(reading) end end + + describe 'destroy' do + context 'when attributes exist' do + let!(:reading_group_attributes) { FactoryGirl.build(:reading_group_attributes) } + let!(:reading_group) { reading_group_attributes.reading_group } + + it 'should be destroyed' do + reading_group.expects(:attributes).twice.returns(reading_group_attributes) + reading_group_attributes.expects(:destroy) + KalibroClient::Entities::Configurations::ReadingGroup.any_instance.expects(:destroy).returns(reading_group) + reading_group.destroy + end + + it 'is expected to clean the attributes memoization' do + # Call attributes once so it memoizes + ReadingGroupAttributes.expects(:find_by).with(reading_group_id: reading_group.id).returns(reading_group_attributes) + expect(reading_group.attributes).to eq(reading_group_attributes) + + # Destroying + reading_group.destroy + + # The expectation call will try to find the attributes on the database which should be nil since it was destroyed + ReadingGroupAttributes.expects(:find_by).with(reading_group_id: reading_group.id).returns(nil) + expect(reading_group.attributes).to be_nil + end + end + end end describe 'class methods' do @@ -86,8 +113,6 @@ describe ReadingGroup, :type => :model do end it 'should find all public and owned reading groups' do - p all_reading_groups - expect(ReadingGroup.public_or_owned_by_user(user)).to eq(owned_or_public_reading_groups) end end -- libgit2 0.21.2