From ccbf20a73bba5792cf78c98cfa646fb4ab6d88fe Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Tue, 13 Sep 2022 15:47:53 +1000 Subject: [PATCH] Replace spec of customer's internal logic Specs should test public methods. Private methods should be able to change without specs breaking. The specs were also just mirroring the code and didn't really have any meaning. So I wrote new ones which are hopefully clearer. There's actually a case which I don't quite understand. Why don't we require a customer for a new complete order? It means that we don't have a customer creating it but then it's added when updating the order. Seems inconsistent. --- spec/models/spree/order_spec.rb | 48 +++++++++++---------------------- 1 file changed, 15 insertions(+), 33 deletions(-) diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index c490457aa3..808174c04a 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -893,45 +893,27 @@ describe Spree::Order do end end - describe "#require_customer?" do - context "when the record is new" do - let(:order) { build(:order, state: state) } - - context "and the state is not cart" do - let(:state) { "complete" } - - it "returns false" do - expect(order.send(:require_customer?)).to eq(false) - end - end - - context "and the state is cart" do - let(:state) { "cart" } - - it "returns false" do - expect(order.send(:require_customer?)).to eq(false) - end - end + describe "#customer" do + it "is not required for new records" do + is_expected.to_not validate_presence_of(:customer) end - context "when the record is not new" do - let(:order) { create(:order, state: state) } + it "is not required for new complete orders" do + order = Spree::Order.new(state: "complete") - context "and the state is not cart" do - let(:state) { "complete" } + expect(order).to_not validate_presence_of(:customer) + end - it "returns true" do - expect(order.send(:require_customer?)).to eq(true) - end - end + it "is not required for existing orders in cart state" do + order = create(:order) - context "and the state is cart" do - let(:state) { "cart" } + expect(order).to_not validate_presence_of(:customer) + end - it "returns false" do - expect(order.send(:require_customer?)).to eq(false) - end - end + it "is created for existing orders in complete state" do + order = create(:order, state: "complete") + + expect { order.valid? }.to change { order.customer }.from(nil) end end