Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

add testcase checking code length, give up on mocking random

Mateusz Porębskib4ff5c1661fa
diff --git a/core/spec/services/spree/promotion_batches/generate_code_spec.rb b/core/spec/services/spree/promotion_batches/generate_code_spec.rb
index f3848296c3..e30f9982a3 100644
--- a/core/spec/services/spree/promotion_batches/generate_code_spec.rb
+++ b/core/spec/services/spree/promotion_batches/generate_code_spec.rb
@@ -2,20 +2,19 @@ require 'spec_helper'
module Spree
describe PromotionBatches::GenerateCode do
- subject { described_class.new(random: random_double).call(random_characters: random_characters, prefix: prefix, suffix: suffix) }
+ subject { described_class.new.call(random_characters: random_characters, prefix: prefix, suffix: suffix) }
let(:random_double) { double }
let(:random_characters) { 4 }
let(:prefix) { nil }
let(:suffix) { nil }
- before do
- allow(random_double).to receive(:hex).with(random_characters).and_return('code')
- end
-
context 'when no prefix or suffix is set' do
it 'returns randomly generated code in uppercase' do
- expect(subject).to eq('CODE')
+ expect(subject).to match('^[A-Z0-9]*$')
+ end
+ it 'returns randomly generated code with correct length' do
+ expect(subject.length).to eq(4)
end
end
@@ -23,7 +22,7 @@ module Spree
let(:prefix) { 'MYCAMPAIGN_' }
it 'returns prefix and randomly generated code' do
- expect(subject).to eq('MYCAMPAIGN_CODE')
+ expect(subject).to start_with('MYCAMPAIGN_')
end
end
@@ -31,7 +30,7 @@ module Spree
let(:suffix) { '_RETAIL' }
it 'returns randomly generated code with suffix' do
- expect(subject).to eq('CODE_RETAIL')
+ expect(subject).to end_with('_RETAIL')
end
end
@@ -40,7 +39,8 @@ module Spree
let(:suffix) { '_ONLINE' }
it 'returns randomly generated code with prefix and suffix' do
- expect(subject).to eq('TESTCAMPAIGN_CODE_ONLINE')
+ expect(subject).to start_with('TESTCAMPAIGN_')
+ expect(subject).to end_with('_ONLINE')
end
end
end