diff --git a/Gemfile b/Gemfile index 682b445502..b4fbd69607 100644 --- a/Gemfile +++ b/Gemfile @@ -156,6 +156,7 @@ end group :test do gem 'simplecov', require: false + gem 'test-prof' gem 'webmock' # See spec/spec_helper.rb for instructions # gem 'perftools.rb' diff --git a/Gemfile.lock b/Gemfile.lock index 2c7101228c..9d39c878c0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -661,6 +661,7 @@ GEM stringex (1.5.1) stripe (5.25.0) temple (0.8.2) + test-prof (0.7.5) test-unit (3.3.6) power_assert thor (0.20.3) @@ -811,6 +812,7 @@ DEPENDENCIES state_machine (= 1.2.0) stringex (~> 1.5.1) stripe + test-prof test-unit (~> 3.3) timecop truncate_html (= 0.9.2) diff --git a/spec/factories/enterprise_factory.rb b/spec/factories/enterprise_factory.rb index 2504ccb815..2e949eb699 100644 --- a/spec/factories/enterprise_factory.rb +++ b/spec/factories/enterprise_factory.rb @@ -6,12 +6,12 @@ FactoryBot.define do promo_image {} end - owner { FactoryBot.create :user } + owner factory: :user sequence(:name) { |n| "Enterprise #{n}" } sells 'any' description 'enterprise' long_description '
Hello, world!
This is a paragraph.
' - address { FactoryBot.create(:address) } + address after(:create) do |enterprise, proxy| proxy.users.each do |user| diff --git a/spec/factories/tag_rule_factory.rb b/spec/factories/tag_rule_factory.rb index 4a4f5b1130..bcfa1783d8 100644 --- a/spec/factories/tag_rule_factory.rb +++ b/spec/factories/tag_rule_factory.rb @@ -1,18 +1,18 @@ FactoryBot.define do factory :filter_order_cycles_tag_rule, class: TagRule::FilterOrderCycles do - enterprise { FactoryBot.create :distributor_enterprise } + enterprise factory: :distributor_enterprise end factory :filter_shipping_methods_tag_rule, class: TagRule::FilterShippingMethods do - enterprise { FactoryBot.create :distributor_enterprise } + enterprise factory: :distributor_enterprise end factory :filter_products_tag_rule, class: TagRule::FilterProducts do - enterprise { FactoryBot.create :distributor_enterprise } + enterprise factory: :distributor_enterprise end factory :filter_payment_methods_tag_rule, class: TagRule::FilterPaymentMethods do - enterprise { FactoryBot.create :distributor_enterprise } + enterprise factory: :distributor_enterprise end factory :tag_rule, class: TagRule::DiscountOrder do diff --git a/spec/models/order_cycle_spec.rb b/spec/models/order_cycle_spec.rb index e329297f91..c289f7871b 100644 --- a/spec/models/order_cycle_spec.rb +++ b/spec/models/order_cycle_spec.rb @@ -288,7 +288,7 @@ describe OrderCycle do end describe "checking status" do - let(:oc) { create(:simple_order_cycle) } + let(:oc) { build_stubbed(:simple_order_cycle) } it "reports status when an order cycle is upcoming" do Timecop.freeze(oc.orders_open_at - 1.second) do @@ -319,7 +319,8 @@ describe OrderCycle do end it "reports status when an order cycle is undated" do - oc.update!(orders_open_at: nil, orders_close_at: nil) + oc.orders_open_at = nil + oc.orders_close_at = nil expect(oc).to be_undated expect(oc).not_to be_dated @@ -329,7 +330,7 @@ describe OrderCycle do end it "reports status when an order cycle is partially dated - opening time only" do - oc.update!(orders_close_at: nil) + oc.orders_close_at = nil expect(oc).to be_undated expect(oc).not_to be_dated @@ -339,7 +340,7 @@ describe OrderCycle do end it "reports status when an order cycle is partially dated - closing time only" do - oc.update!(orders_open_at: nil) + oc.orders_open_at = nil expect(oc).to be_undated expect(oc).not_to be_dated diff --git a/spec/models/spree/classification_spec.rb b/spec/models/spree/classification_spec.rb index 4d0f089e56..2eb3fd68e1 100644 --- a/spec/models/spree/classification_spec.rb +++ b/spec/models/spree/classification_spec.rb @@ -2,11 +2,11 @@ require 'spec_helper' module Spree describe Classification do - let!(:product) { create(:simple_product) } - let!(:taxon) { create(:taxon) } - let(:classification) { create(:classification, taxon: taxon, product: product) } + let(:product) { build_stubbed(:simple_product) } + let(:taxon) { build_stubbed(:taxon) } it "won't destroy if classification is the primary taxon" do + classification = Classification.new(taxon: taxon, product: product) product.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"]) diff --git a/spec/models/spree/order/checkout_spec.rb b/spec/models/spree/order/checkout_spec.rb index 1020fb0fe5..dcaa58f504 100644 --- a/spec/models/spree/order/checkout_spec.rb +++ b/spec/models/spree/order/checkout_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Spree::Order do +describe Spree::Order::Checkout do let(:order) { Spree::Order.new } context "with default state machine" do @@ -15,8 +15,6 @@ describe Spree::Order do it "has the following transitions" do transitions.each do |transition| - puts transition.keys.first - puts transition.values.first transition = Spree::Order.find_transition(from: transition.keys.first, to: transition.values.first) expect(transition).to_not be_nil @@ -302,12 +300,12 @@ describe Spree::Order do end describe 'event :restart_checkout' do - let(:order) { create(:order) } + let(:order) { build_stubbed(:order) } context 'when the order is not complete' do before { allow(order).to receive(:completed?) { false } } - it 'does transition to cart state' do + it 'transitions to cart state' do expect(order.state).to eq('cart') end end diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index d3d901fb5f..608e6c0eca 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -54,7 +54,7 @@ describe Spree::Order do end it "does nothing when the line item is not found" do - p = create(:simple_product) + p = build_stubbed(:simple_product) subject.set_variant_attributes(p.master, { 'max_quantity' => '3' }.with_indifferent_access) end end @@ -823,10 +823,14 @@ describe Spree::Order do end describe '#restart_checkout!' do - let(:order) { build(:order, line_items: [build(:line_item)]) } - context 'when the order is complete' do - before { order.completed_at = Time.zone.now } + let(:order) do + build_stubbed( + :order, + completed_at: Time.zone.now, + line_items: [build_stubbed(:line_item)] + ) + end it 'raises' do expect { order.restart_checkout! } @@ -835,7 +839,13 @@ describe Spree::Order do end context 'when the is not complete' do - before { order.completed_at = nil } + let(:order) do + build( + :order, + completed_at: nil, + line_items: [build(:line_item)] + ) + end it 'transitions to :cart state' do order.restart_checkout! diff --git a/spec/models/tag_rule/filter_order_cycles_spec.rb b/spec/models/tag_rule/filter_order_cycles_spec.rb index 56153e704b..e2dc07906d 100644 --- a/spec/models/tag_rule/filter_order_cycles_spec.rb +++ b/spec/models/tag_rule/filter_order_cycles_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe TagRule::FilterOrderCycles, type: :model do - let!(:tag_rule) { create(:filter_order_cycles_tag_rule) } + let!(:tag_rule) { build_stubbed(:filter_order_cycles_tag_rule) } describe "determining whether tags match for a given exchange" do context "when the exchange is nil" do diff --git a/spec/models/tag_rule/filter_payment_methods_spec.rb b/spec/models/tag_rule/filter_payment_methods_spec.rb index 6b61ba0230..63312ca358 100644 --- a/spec/models/tag_rule/filter_payment_methods_spec.rb +++ b/spec/models/tag_rule/filter_payment_methods_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe TagRule::FilterPaymentMethods, type: :model do - let!(:tag_rule) { create(:filter_payment_methods_tag_rule) } + let!(:tag_rule) { build_stubbed(:filter_payment_methods_tag_rule) } describe "determining whether tags match for a given payment method" do context "when the payment method is nil" do diff --git a/spec/models/tag_rule/filter_products_spec.rb b/spec/models/tag_rule/filter_products_spec.rb index 0519dc00dc..241723157b 100644 --- a/spec/models/tag_rule/filter_products_spec.rb +++ b/spec/models/tag_rule/filter_products_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe TagRule::FilterProducts, type: :model do - let!(:tag_rule) { create(:filter_products_tag_rule) } + let!(:tag_rule) { build_stubbed(:filter_products_tag_rule) } describe "determining whether tags match for a given variant" do context "when the variant is nil" do diff --git a/spec/models/tag_rule/filter_shipping_methods_spec.rb b/spec/models/tag_rule/filter_shipping_methods_spec.rb index 58a1ee4854..6a52e4b679 100644 --- a/spec/models/tag_rule/filter_shipping_methods_spec.rb +++ b/spec/models/tag_rule/filter_shipping_methods_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe TagRule::FilterShippingMethods, type: :model do - let!(:tag_rule) { create(:filter_shipping_methods_tag_rule) } + let!(:tag_rule) { build_stubbed(:filter_shipping_methods_tag_rule) } describe "determining whether tags match for a given shipping method" do context "when the shipping method is nil" do @@ -11,7 +11,7 @@ describe TagRule::FilterShippingMethods, type: :model do end context "when the shipping method is not nil" do - let(:shipping_method) { create(:shipping_method, tag_list: ["member", "local", "volunteer"]) } + let(:shipping_method) { build_stubbed(:shipping_method, tag_list: ["member", "local", "volunteer"]) } context "when the rule has no preferred shipping method tags specified" do before { allow(tag_rule).to receive(:preferred_shipping_method_tags) { "" } } diff --git a/spec/models/variant_override_spec.rb b/spec/models/variant_override_spec.rb index 678ccdfee9..f7123b31ef 100644 --- a/spec/models/variant_override_spec.rb +++ b/spec/models/variant_override_spec.rb @@ -42,10 +42,15 @@ describe VariantOverride do describe "validation" do describe "ensuring that on_demand and count_on_hand are compatible" do - let(:variant_override) { - build(:variant_override, hub: hub, variant: variant, - on_demand: on_demand, count_on_hand: count_on_hand) - } + let(:variant_override) do + build_stubbed( + :variant_override, + hub: build_stubbed(:distributor_enterprise), + variant: build_stubbed(:variant), + on_demand: on_demand, + count_on_hand: count_on_hand + ) + end context "when using producer stock settings" do let(:on_demand) { nil } @@ -62,7 +67,7 @@ describe VariantOverride do let(:count_on_hand) { 1 } it "is invalid" do - expect(variant_override.save).to be_falsey + expect(variant_override).not_to be_valid error_message = I18n.t("using_producer_stock_settings_but_count_on_hand_set", scope: [i18n_scope_for_error, "count_on_hand"]) expect(variant_override.errors[:count_on_hand]).to eq([error_message]) @@ -139,7 +144,14 @@ describe VariantOverride do end describe "with price" do - let(:variant_override) { build_stubbed(:variant_override, variant: variant, hub: hub, price: 12.34) } + let(:variant_override) do + build_stubbed( + :variant_override, + variant: build_stubbed(:variant), + hub: build_stubbed(:distributor_enterprise), + price: 12.34 + ) + end it "returns the numeric price" do expect(variant_override.price).to eq(12.34) @@ -147,7 +159,15 @@ describe VariantOverride do end describe "with nil count on hand" do - let(:variant_override) { build_stubbed(:variant_override, variant: variant, hub: hub, count_on_hand: nil, on_demand: true) } + let(:variant_override) do + build_stubbed( + :variant_override, + variant: build_stubbed(:variant), + hub: build_stubbed(:distributor_enterprise), + count_on_hand: nil, + on_demand: true + ) + end describe "stock_overridden?" do it "returns false" do @@ -164,7 +184,14 @@ describe VariantOverride do end describe "with count on hand" do - let(:variant_override) { create(:variant_override, variant: variant, hub: hub, count_on_hand: 12) } + let(:variant_override) do + build_stubbed( + :variant_override, + variant: build_stubbed(:variant), + hub: build_stubbed(:distributor_enterprise), + count_on_hand: 12 + ) + end it "returns the numeric count on hand" do expect(variant_override.count_on_hand).to eq(12) @@ -177,6 +204,15 @@ describe VariantOverride do end describe "move_stock!" do + let(:variant_override) do + create( + :variant_override, + variant: variant, + hub: hub, + count_on_hand: 12 + ) + end + it "does nothing for quantity zero" do variant_override.move_stock!(0) expect(variant_override.reload.count_on_hand).to eq(12) @@ -196,12 +232,24 @@ describe VariantOverride do describe "checking default stock value is present" do it "returns true when a default stock level has been set" do - vo = build_stubbed(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock: 20) + vo = build_stubbed( + :variant_override, + variant: build_stubbed(:variant), + hub: build_stubbed(:distributor_enterprise), + count_on_hand: 12, + default_stock: 20 + ) expect(vo.default_stock?).to be true end it "returns false when the override has no default stock level" do - vo = build_stubbed(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock: nil) + vo = build_stubbed( + :variant_override, + variant: build_stubbed(:variant), + hub: build_stubbed(:distributor_enterprise), + count_on_hand: 12, + default_stock: nil + ) expect(vo.default_stock?).to be false end end