From ab4660ecd0c105ea7748fcbdbd9fb5aabf76ee63 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Sat, 10 Apr 2021 17:37:58 +0100 Subject: [PATCH] Fix callback in Classification and update spec Fixes: 2) Spree::Classification won't destroy if classification is the primary taxon (Used from /home/runner/work/openfoodnetwork/openfoodnetwork/spec/models/enterprise_group_spec.rb:43:in `block (3 levels) in ') Failure/Error: expect(classification.destroy).to be false --- app/models/spree/classification.rb | 2 +- spec/models/spree/classification_spec.rb | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/models/spree/classification.rb b/app/models/spree/classification.rb index 0b668a068b..b4e78214a3 100644 --- a/app/models/spree/classification.rb +++ b/app/models/spree/classification.rb @@ -15,7 +15,7 @@ module Spree errors.add :base, I18n.t(:spree_classification_primary_taxon_error, taxon: taxon.name, product: product.name) - false + throw :abort end end end diff --git a/spec/models/spree/classification_spec.rb b/spec/models/spree/classification_spec.rb index 65407747cc..6a35477f12 100644 --- a/spec/models/spree/classification_spec.rb +++ b/spec/models/spree/classification_spec.rb @@ -4,14 +4,16 @@ require 'spec_helper' module Spree describe Classification do - let(:product) { build_stubbed(:simple_product) } - let(:taxon) { build_stubbed(:taxon) } + let(:product) { create(:simple_product) } + let(:taxon) { create(:taxon) } it "won't destroy if classification is the primary taxon" do - classification = Classification.new(taxon: taxon, product: product) - product.primary_taxon = taxon + classification = Classification.create(taxon: taxon, product: product) + product.update(primary_taxon: taxon) + expect(classification.destroy).to be false expect(classification.errors.messages[:base]).to eq(["Taxon #{taxon.name} is the primary taxon of #{product.name} and cannot be deleted"]) + expect(classification.reload).to be end end end