Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable partial doubles verification for RSpec #3005

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
313089f
Enable partial double verification for tests in Core
cedum Dec 7, 2018
644b7f2
Remove regression tests for inexistent cases
cedum Dec 7, 2018
4dfafa8
Remove unimplemented Spree::Order#finalize! tests
cedum Dec 13, 2018
7d531ab
Fix initial order object for Spree::Order#finalize! specs
cedum Dec 13, 2018
187499e
Fix a method name in a Return Item test example
cedum Dec 13, 2018
397cbb7
Fix update shipments mock in a OrderUpdater test
cedum Dec 13, 2018
7d4ed88
Fix a mock in a OrderUpdater test example
cedum Dec 13, 2018
bf3aa76
Fix method name typo in Spree::Payment specs
cedum Dec 13, 2018
2d0984a
Disable partial double check for OrderMerger specs
cedum Dec 13, 2018
ce76c3c
Add RSpec tag to disble partial double verification
cedum Dec 17, 2018
b64fb1d
Upgrade rspec-activemodel-mocks to ~>1.1
cedum Dec 17, 2018
6a3896f
Remove unimplemented stub from a InventoryUnit test
cedum Dec 17, 2018
cc19bb4
Disable double verification for some test examples
cedum Dec 17, 2018
4971602
Fix stock estimator mocked sorter class initializer
cedum Dec 17, 2018
bf60389
Disable double verification in order checkout specs
cedum Dec 17, 2018
fd0ff05
Remove unimplemented mocked methods for some tests
cedum Dec 17, 2018
9ae0e0a
Reuse already defined object in a Promotion test
cedum Dec 17, 2018
bd0d91e
Refactor some Spree::Promotion specs
cedum Dec 17, 2018
0b8553a
Refactor test for Shipment assoc. to InventoryUnit
cedum Dec 17, 2018
cff9df5
Disable double verification for a Shipment test
cedum Dec 17, 2018
f7d6814
Fix a Payment::Cancellation test case
cedum Dec 17, 2018
c4e5055
Disable double verification in some helpers specs
cedum Dec 17, 2018
11e3ca4
Allow stub of unimplmented helper method
cedum Dec 17, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ end
gem 'database_cleaner', '~> 1.3', require: false
gem 'factory_bot_rails', '~> 4.8', require: false
gem 'i18n-tasks', '~> 0.9', require: false
gem 'rspec-activemodel-mocks', '~>1.0.2', require: false
gem 'rspec-activemodel-mocks', '~>1.1', require: false
gem 'rspec-rails', '~> 3.7', require: false
gem 'simplecov', require: false
gem 'with_model', require: false
Expand Down
4 changes: 3 additions & 1 deletion core/spec/helpers/products_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ module Spree
end

before do
allow(helper).to receive(:current_pricing_options) { pricing_options }
without_partial_double_verification do
allow(helper).to receive(:current_pricing_options) { pricing_options }
end
end

context "#variant_price_diff" do
Expand Down
4 changes: 3 additions & 1 deletion core/spec/helpers/taxons_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
Spree::Config.pricing_options_class.new(currency: currency)
end
before do
allow(helper).to receive(:current_pricing_options) { pricing_options }
without_partial_double_verification do
allow(helper).to receive(:current_pricing_options) { pricing_options }
end
end

describe "#taxon_preview" do
Expand Down
8 changes: 6 additions & 2 deletions core/spec/lib/spree/core/controller_helpers/auth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ def index

describe '#try_spree_current_user' do
it 'calls spree_current_user when define spree_current_user method' do
expect(controller).to receive(:spree_current_user)
without_partial_double_verification do
expect(controller).to receive(:spree_current_user)
end
controller.try_spree_current_user
end
it 'calls current_spree_user when define current_spree_user method' do
expect(controller).to receive(:current_spree_user)
without_partial_double_verification do
expect(controller).to receive(:current_spree_user)
end
controller.try_spree_current_user
end
it 'returns nil' do
Expand Down
4 changes: 0 additions & 4 deletions core/spec/models/spree/inventory_unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@
shipment.stock_location = stock_location
shipment.shipping_methods << create(:shipping_method)
shipment.order = other_order
# We don't care about this in this test
allow(shipment).to receive(:ensure_correct_adjustment)
shipment.tap(&:save!)
end

Expand Down Expand Up @@ -149,8 +147,6 @@
end

describe "#current_or_new_return_item" do
before { allow(inventory_unit).to receive_messages(total_excluding_vat: 100.0) }

subject { inventory_unit.current_or_new_return_item }

context "associated with a return item" do
Expand Down
12 changes: 6 additions & 6 deletions core/spec/models/spree/order/checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def assert_state_changed(order, from, to)
Spree::Order.checkout_flow(&@old_checkout_flow)
end

it '.remove_transition' do
it '.remove_transition', partial_double_verification: false do
options = { from: transitions.first.keys.first, to: transitions.first.values.first }
allow(Spree::Order).to receive(:next_event_transition).and_return([options])
expect(Spree::Order.remove_transition(options)).to be_truthy
Expand Down Expand Up @@ -238,7 +238,7 @@ def assert_state_changed(order, from, to)
order.ship_address = ship_address
end

context 'when order has default selected_shipping_rate_id' do
context 'when order has default selected_shipping_rate_id', partial_double_verification: false do
let(:shipment) { create(:shipment, order: order) }
let(:shipping_method) { create(:shipping_method) }
let(:shipping_rate) {
Expand Down Expand Up @@ -277,7 +277,7 @@ def assert_state_changed(order, from, to)
end
end

context "from delivery" do
context "from delivery", partial_double_verification: false do
let(:ship_address) { FactoryBot.create(:ship_address) }

before do
Expand Down Expand Up @@ -537,7 +537,7 @@ def assert_state_changed(order, from, to)
end
end

context "default credit card" do
context "default credit card", partial_double_verification: false do
before do
order.user = FactoryBot.create(:user)
order.store = FactoryBot.create(:store)
Expand Down Expand Up @@ -568,7 +568,7 @@ def assert_state_changed(order, from, to)
end
end

context "a payment fails during processing" do
context "a payment fails during processing", partial_double_verification: false do
before do
order.user = FactoryBot.create(:user)
order.email = 'spree@example.org'
Expand Down Expand Up @@ -656,7 +656,7 @@ def assert_state_changed(order, from, to)
assert_state_changed(order, 'cart', 'complete')
end

it "does not attempt to process payments" do
it "does not attempt to process payments", partial_double_verification: false do
order.email = 'user@example.com'
allow(order).to receive(:ensure_promotions_eligible).and_return(true)
allow(order).to receive(:ensure_line_item_variants_are_not_deleted).and_return(true)
Expand Down
21 changes: 1 addition & 20 deletions core/spec/models/spree/order/finalizing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
require 'rails_helper'

RSpec.describe Spree::Order, type: :model do
let(:order) { stub_model("Spree::Order") }

context "#finalize!" do
let!(:store) { create(:store) }
let(:order) { Spree::Order.create(email: 'test@example.com', store: store) }
let(:order) { create(:order_ready_to_complete) }

before do
order.update_column :state, 'complete'
Expand All @@ -26,29 +23,13 @@
order.finalize!
end

it "should decrease the stock for each variant in the shipment" do
order.shipments.each do |shipment|
expect(shipment.stock_location).to receive(:decrease_stock_for_variant)
end
order.finalize!
end

it "should change the shipment state to ready if order is paid" do
Spree::Shipment.create(order: order)
order.shipments.reload

allow(order).to receive_messages(paid?: true, complete?: true)
order.finalize!
order.reload # reload so we're sure the changes are persisted
expect(order.shipment_state).to eq('ready')
end

it "should not sell inventory units if track_inventory_levels is false" do
Spree::Config.set track_inventory_levels: false
expect(Spree::InventoryUnit).not_to receive(:sell_units)
order.finalize!
end

it "should send an order confirmation email" do
mail_message = double "Mail::Message"
expect(Spree::OrderMailer).to receive(:confirm_email).with(order).and_return mail_message
Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/order/updating_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
let(:order) { create(:order) }

context "#update!" do
context "when there are update hooks" do
context "when there are update hooks", partial_double_verification: false do
before { Spree::Order.register_update_hook :foo }
after { Spree::Order.update_hooks.clear }
it "should call each of the update hooks" do
Expand Down
9 changes: 6 additions & 3 deletions core/spec/models/spree/order_merger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ module Spree
context "merging using extension-specific line_item_comparison_hooks" do
before do
Spree::Order.register_line_item_comparison_hook(:foos_match)
allow(Spree::Variant).to receive(:price_modifier_amount).and_return(0.00)
end

after do
Expand All @@ -83,7 +82,9 @@ module Spree
end

specify do
expect(order_1).to receive(:foos_match).with(@line_item_1, kind_of(Hash)).and_return(true)
without_partial_double_verification do
expect(order_1).to receive(:foos_match).with(@line_item_1, kind_of(Hash)).and_return(true)
end
subject.merge!(order_2)
expect(order_1.line_items.count).to eq(1)

Expand All @@ -95,7 +96,9 @@ module Spree

context "2 different line items" do
before do
allow(order_1).to receive(:foos_match).and_return(false)
without_partial_double_verification do
allow(order_1).to receive(:foos_match).and_return(false)
end

order_1.contents.add(variant, 1, foos: {})
order_2.contents.add(variant, 1, foos: { bar: :zoo })
Expand Down
10 changes: 3 additions & 7 deletions core/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
end
let(:code) { promotion.codes.first }

before do
allow(Spree::LegacyUser).to receive_messages(current: mock_model(Spree::LegacyUser, id: 123))
end

context '#store' do
it { is_expected.to respond_to(:store) }

Expand Down Expand Up @@ -324,7 +320,7 @@ def merge!(other_order, user = nil)
end
end

context "add_update_hook" do
context "add_update_hook", partial_double_verification: false do
before do
Spree::Order.class_eval do
register_update_hook :add_awesome_sauce
Expand Down Expand Up @@ -712,7 +708,7 @@ def merge!(other_order, user = nil)
expect(order.find_line_item_by_variant(mock_model(Spree::Variant))).to be_nil
end

context "match line item with options" do
context "match line item with options", partial_double_verification: false do
before do
Spree::Order.register_line_item_comparison_hook(:foos_match)
end
Expand Down Expand Up @@ -1072,7 +1068,7 @@ def generate

context 'an old-style refund exists' do
let(:order) { create(:order_ready_to_ship) }
let(:payment) { order.payments.first.tap { |p| allow(p).to receive_messages(profiles_supported: false) } }
let(:payment) { order.payments.first.tap { |p| allow(p).to receive_messages(profiles_supported?: false) } }
let!(:refund_payment) {
build(:payment, amount: -1, order: order, state: 'completed', source: payment).tap do |p|
allow(p).to receive_messages(profiles_supported?: false)
Expand Down
11 changes: 3 additions & 8 deletions core/spec/models/spree/order_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -513,15 +513,10 @@ def create_adjustment(label, amount)

it "doesnt update each shipment" do
shipment = stub_model(Spree::Shipment)
shipments = [shipment]
allow(order).to receive_messages shipments: shipments
allow(shipments).to receive_messages states: []
allow(shipments).to receive_messages ready: []
allow(shipments).to receive_messages pending: []
allow(shipments).to receive_messages shipped: []

order.shipments = [shipment]
allow(order.shipments).to receive_messages(states: [], ready: [], pending: [], shipped: [])
allow(updater).to receive(:update_totals) # Otherwise this gets called and causes a scene
expect(updater).not_to receive(:update_shipments).with(order)
expect(updater).not_to receive(:update_shipments)
updater.update
end
end
Expand Down
7 changes: 4 additions & 3 deletions core/spec/models/spree/payment/cancellation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@
end
end

context 'if the payment_method does not respond to `try_void`' do
context 'if the payment_method does not respond to `try_void`', partial_double_verification: false do
before do
allow(payment_method).to receive(:respond_to?) { false }
expect(payment_method).to receive(:cancel) { double }
expect(payment).to receive(:handle_void_response)
allow(payment_method).to receive(:cancel) { double }
allow(payment).to receive(:handle_void_response)
end

it 'calls cancel instead' do
expect(payment_method).to receive(:cancel)
Spree::Deprecation.silence { subject }
end

Expand Down
18 changes: 1 addition & 17 deletions core/spec/models/spree/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

let(:gateway) do
gateway = Spree::PaymentMethod::BogusCreditCard.new(active: true, name: 'Bogus gateway')
allow(gateway).to receive_messages source_required: true
allow(gateway).to receive_messages(source_required?: true)
gateway
end

Expand Down Expand Up @@ -207,22 +207,6 @@
expect { payment.process! }.to raise_error(Spree::Core::GatewayError)
expect(payment.state).to eq('invalid')
end

# Regression test for https://github.com/spree/spree/issues/4598
it "should allow payments with a gateway_customer_profile_id" do
payment.source.update!(gateway_customer_profile_id: "customer_1", brand: 'visa')
expect(payment.payment_method.gateway_class).to receive(:supports?).with('visa').and_return(false)
expect(payment).to receive(:started_processing!)
payment.process!
end

# Another regression test for https://github.com/spree/spree/issues/4598
it "should allow payments with a gateway_payment_profile_id" do
payment.source.update!(gateway_payment_profile_id: "customer_1", brand: 'visa')
expect(payment.payment_method.gateway_class).to receive(:supports?).with('visa').and_return(false)
expect(payment).to receive(:started_processing!)
payment.process!
end
end

describe "#authorize!" do
Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Extension < Spree::Base
expect(clone.images.size).to eq(product.images.size)
end

it 'calls #duplicate_extra' do
it 'calls #duplicate_extra', partial_double_verification: false do
expect_any_instance_of(Spree::Product).to receive(:duplicate_extra) do |product, old_product|
product.name = old_product.name.reverse
end
Expand Down
21 changes: 11 additions & 10 deletions core/spec/models/spree/promotion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -632,15 +632,16 @@
end

context "with 'any' match policy" do
let(:promotion) { Spree::Promotion.create(name: "Promo", match_policy: 'any') }
let(:promotable) { double('Promotable') }

before do
promotion.match_policy = 'any'
end

it "should have eligible rules if any of the rules are eligible" do
allow_any_instance_of(Spree::PromotionRule).to receive_messages(applicable?: true)
true_rule = Spree::PromotionRule.create(promotion: promotion)
allow(true_rule).to receive_messages(eligible?: true)
allow(promotion).to receive_messages(rules: [true_rule])
allow(promotion).to receive_message_chain(:rules, :for).and_return([true_rule])
true_rule = mock_model(Spree::PromotionRule, eligible?: true, applicable?: true)
promotion.promotion_rules = [true_rule]
allow(promotion.rules).to receive(:for) { promotion.rules }
expect(promotion.eligible_rules(promotable)).to eq [true_rule]
end

Expand Down Expand Up @@ -668,13 +669,13 @@
describe '#line_item_actionable?' do
let(:order) { double Spree::Order }
let(:line_item) { double Spree::LineItem }
let(:true_rule) { double Spree::PromotionRule, eligible?: true, applicable?: true, actionable?: true }
let(:false_rule) { double Spree::PromotionRule, eligible?: true, applicable?: true, actionable?: false }
let(:true_rule) { mock_model Spree::PromotionRule, eligible?: true, applicable?: true, actionable?: true }
let(:false_rule) { mock_model Spree::PromotionRule, eligible?: true, applicable?: true, actionable?: false }
let(:rules) { [] }

before do
allow(promotion).to receive(:rules) { rules }
allow(rules).to receive(:for) { rules }
promotion.promotion_rules = rules
allow(promotion.rules).to receive(:for) { rules }
end

subject { promotion.line_item_actionable? order, line_item }
Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/return_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@
end

it 'does not decrease inventory' do
expect(return_item).to_not receive(:process_inventory_unit)
expect(return_item).to_not receive(:process_inventory_unit!)
subject
end

Expand Down
Loading