From db029964fad186ff940107c64c092986a5583d3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Tue, 13 Dec 2016 18:38:13 +0100 Subject: [PATCH] Check geozone 'safe_to_delete' behavior as unit test instead of integration test --- spec/features/admin/geozones_spec.rb | 47 +--------------------------- spec/models/geozone_spec.rb | 26 +++++++++++++++ 2 files changed, 27 insertions(+), 46 deletions(-) diff --git a/spec/features/admin/geozones_spec.rb b/spec/features/admin/geozones_spec.rb index cb0392663..44f9b0d03 100644 --- a/spec/features/admin/geozones_spec.rb +++ b/spec/features/admin/geozones_spec.rb @@ -83,7 +83,7 @@ feature 'Admin geozones' do expect(Geozone.where(id: geozone.id)).to be_empty end - scenario 'Delete geozone with associated proposal' do + scenario 'Delete geozone with associated element' do geozone = create(:geozone, name: 'Delete me!') create(:proposal, geozone: geozone) @@ -97,49 +97,4 @@ feature 'Admin geozones' do expect(page).to have_content 'Delete me!' end end - - scenario 'Delete geozone with associated spending proposal' do - geozone = create(:geozone, name: 'Delete me!') - create(:spending_proposal, geozone: geozone) - - visit admin_geozones_path - - within("#geozone_#{geozone.id}") { click_link 'Delete' } - - expect(page).to have_content "This geozone can't be deleted since there are elements attached to it" - - within("#geozone_#{geozone.id}") do - expect(page).to have_content 'Delete me!' - end - end - - scenario 'Delete geozone with associated debate' do - geozone = create(:geozone, name: 'Delete me!') - create(:debate, geozone: geozone) - - visit admin_geozones_path - - within("#geozone_#{geozone.id}") { click_link 'Delete' } - - expect(page).to have_content "This geozone can't be deleted since there are elements attached to it" - - within("#geozone_#{geozone.id}") do - expect(page).to have_content 'Delete me!' - end - end - - scenario 'Delete geozone with associated user' do - geozone = create(:geozone, name: 'Delete me!') - create(:user, geozone: geozone) - - visit admin_geozones_path - - within("#geozone_#{geozone.id}") { click_link 'Delete' } - - expect(page).to have_content "This geozone can't be deleted since there are elements attached to it" - - within("#geozone_#{geozone.id}") do - expect(page).to have_content 'Delete me!' - end - end end diff --git a/spec/models/geozone_spec.rb b/spec/models/geozone_spec.rb index a29c4c918..1a7bf6b09 100644 --- a/spec/models/geozone_spec.rb +++ b/spec/models/geozone_spec.rb @@ -11,4 +11,30 @@ RSpec.describe Geozone, type: :model do geozone.name = nil expect(geozone).to_not be_valid end + + describe "#safe_to_destroy?" do + it "is true when not linked to other models" do + expect(geozone.safe_to_destroy?).to be_truthy + end + + it "is false when already linked to user" do + create(:user, geozone: geozone) + expect(geozone.safe_to_destroy?).to be_falsey + end + + it "is false when already linked to proposal" do + create(:proposal, geozone: geozone) + expect(geozone.safe_to_destroy?).to be_falsey + end + + it "is false when already linked to spending proposal" do + create(:spending_proposal, geozone: geozone) + expect(geozone.safe_to_destroy?).to be_falsey + end + + it "is false when already linked to debate" do + create(:debate, geozone: geozone) + expect(geozone.safe_to_destroy?).to be_falsey + end + end end