Move order setup to new factory traits

This commit is contained in:
Kristina Lim
2019-01-11 08:19:03 +08:00
committed by luisramos0
parent a3aa594c42
commit ce030aeb07
2 changed files with 32 additions and 19 deletions

View File

@@ -449,28 +449,14 @@ describe OrderManagement::Reports::EnterpriseFeeSummary::ReportService do
end
def setup_order(options = {})
target = default_order_options.merge(options)
create(:order, customer: target[:customer], distributor: target[:distributor],
order_cycle: target[:order_cycle],
shipping_method: target[:shipping_method]).tap do |order|
create(:line_item, order: order, variant: target[:variant])
order.reload
end
target_options = default_order_options.merge(options)
create(:order, :with_line_item, target_options)
end
def prepare_order(options = {})
order = setup_order(options)
complete_order(order, options)
order.reload
end
def complete_order(order, options)
order.create_shipment!
create(:payment, state: "checkout", order: order, amount: order.total,
payment_method: options[:payment_method] || payment_method)
order.update_distribution_charge!
while !order.completed? do break unless order.next! end
factory_trait_options = { payment_method: payment_method }
target_options = default_order_options.merge(factory_trait_options).merge(options)
create(:order, :with_line_item, :completed, target_options)
end
def default_variant_options

27
spec/factories/orders.rb Normal file
View File

@@ -0,0 +1,27 @@
FactoryBot.modify do
factory :order do
trait :with_line_item do
transient do
variant { FactoryGirl.create(:variant) }
end
after(:create) do |order, evaluator|
create(:line_item, order: order, variant: evaluator.variant)
end
end
trait :completed do
transient do
payment_method { create(:payment_method, distributors: [distributor]) }
end
after(:create) do |order, evaluator|
order.create_shipment!
create(:payment, state: "checkout", order: order, amount: order.total,
payment_method: evaluator.payment_method)
order.update_distribution_charge!
while !order.completed? do break unless order.next! end
end
end
end
end