Add a voucher factory

As vouchers are getting more complicated, it makes sense to use a
factory to simplify writing test.
This commit is contained in:
Gaetan Craig-Riou
2023-05-01 13:32:33 +10:00
committed by Maikel Linke
parent b427e420ce
commit 2d8fa24862
10 changed files with 26 additions and 13 deletions

View File

@@ -237,7 +237,7 @@ describe SplitCheckoutController, type: :controller do
end
describe "Vouchers" do
let(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor) }
let(:voucher) { create(:voucher, code: 'some_code', enterprise: distributor) }
describe "adding a voucher" do
let(:checkout_params) do

View File

@@ -0,0 +1,8 @@
# frozen_string_literal: true
FactoryBot.define do
factory :voucher do
enterprise { create(:distributor_enterprise) }
amount { rand(200.0) }
end
end

View File

@@ -1432,7 +1432,7 @@ describe Spree::Order do
end
describe "#voucher_adjustments" do
let(:voucher) { Voucher.create(code: 'new_code', enterprise: order.distributor) }
let(:voucher) { create(:voucher, code: 'new_code', enterprise: order.distributor) }
context "when no voucher adjustment" do
it 'returns an empty array' do

View File

@@ -11,7 +11,7 @@ describe Voucher do
end
describe 'validations' do
subject { Voucher.new(code: 'new_code', enterprise: enterprise) }
subject { build(:voucher, code: 'new_code', enterprise: enterprise) }
it { is_expected.to validate_presence_of(:code) }
it { is_expected.to validate_uniqueness_of(:code).scoped_to(:enterprise_id) }
@@ -20,7 +20,7 @@ describe Voucher do
end
describe '#compute_amount' do
subject { Voucher.create(code: 'new_code', enterprise: enterprise, amount: 10) }
subject { create(:voucher, code: 'new_code', enterprise: enterprise, amount: 10) }
let(:order) { create(:order_with_totals) }
@@ -41,7 +41,7 @@ describe Voucher do
describe '#create_adjustment' do
subject(:adjustment) { voucher.create_adjustment(voucher.code, order) }
let(:voucher) { Voucher.create(code: 'new_code', enterprise: enterprise, amount: 25) }
let(:voucher) { create(:voucher, code: 'new_code', enterprise: enterprise, amount: 25) }
let(:order) { create(:order_with_line_items, line_items_count: 3, distributor: enterprise) }
it 'includes the full voucher amount' do

View File

@@ -6,7 +6,7 @@ describe VoucherAdjustmentsController, type: :request do
let(:user) { order.user }
let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) }
let(:order) { create( :order_with_line_items, line_items_count: 1, distributor: distributor) }
let(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor) }
let(:voucher) { create(:voucher, code: 'some_code', enterprise: distributor) }
let!(:adjustment) { voucher.create_adjustment(voucher.code, order) }
before do

View File

@@ -5,7 +5,7 @@ require 'spec_helper'
describe VoucherAdjustmentsService do
describe '.calculate' do
let(:enterprise) { build(:enterprise) }
let(:voucher) { Voucher.create(code: 'new_code', enterprise: enterprise) }
let(:voucher) { create(:voucher, code: 'new_code', enterprise: enterprise, amount: 10) }
context 'when voucher covers the order total' do
subject { order.voucher_adjustments.first }

View File

@@ -23,7 +23,7 @@ describe '
it 'lists enterprise vouchers' do
# Given an enterprise with vouchers
Voucher.create!(enterprise: enterprise, code: voucher_code, amount: amount)
create(:voucher, enterprise: enterprise, code: voucher_code, amount: amount)
# When I go to the enterprise voucher tab
visit edit_admin_enterprise_path(enterprise)

View File

@@ -720,8 +720,9 @@ describe "As a consumer, I want to checkout my order" do
end
context "with voucher available" do
let!(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor, amount: amount) }
let(:amount) { 15 }
let!(:voucher) do
create(:voucher, code: 'some_code', enterprise: distributor, amount: 15)
end
before do
visit checkout_step_path(:payment)
@@ -1112,7 +1113,7 @@ describe "As a consumer, I want to checkout my order" do
end
describe "vouchers" do
let(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor, amount: 6) }
let(:voucher) { create(:voucher, code: 'some_code', enterprise: distributor, amount: 6) }
before do
# Add voucher to the order

View File

@@ -136,7 +136,9 @@ describe "As a consumer, I want to see adjustment breakdown" do
end
context "when using a voucher" do
let!(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor, amount: 10) }
let!(:voucher) do
create(:voucher, code: 'some_code', enterprise: distributor, amount: 10)
end
it "will include a tax included amount on the voucher adjustment" do
visit checkout_step_path(:details)

View File

@@ -144,7 +144,9 @@ describe "As a consumer, I want to see adjustment breakdown" do
end
context "when using a voucher" do
let!(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor, amount: 10) }
let!(:voucher) do
create(:voucher, code: 'some_code', enterprise: distributor, amount: 10)
end
it "will include a tax included amount on the voucher adjustment" do
visit checkout_step_path(:details)