diff options
author | Grzegorz Bizon <grzegorz@gitlab.com> | 2017-12-21 12:38:06 +0300 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2017-12-21 12:38:06 +0300 |
commit | 6df36c9d907d205ab977d5d2f81fb167e0dc969c (patch) | |
tree | 3d4b7682786a532005df732602dfd2067557fd1e /qa/spec/factory | |
parent | 88f41bf61f5602aeca46c31d0b7ba76827543b29 (diff) |
Add support for defining explicit dependencies to QA factories
Diffstat (limited to 'qa/spec/factory')
-rw-r--r-- | qa/spec/factory/base_spec.rb | 88 | ||||
-rw-r--r-- | qa/spec/factory/dependency_spec.rb | 59 | ||||
-rw-r--r-- | qa/spec/factory/product_spec.rb | 44 |
3 files changed, 191 insertions, 0 deletions
diff --git a/qa/spec/factory/base_spec.rb b/qa/spec/factory/base_spec.rb new file mode 100644 index 00000000000..a3ba0176819 --- /dev/null +++ b/qa/spec/factory/base_spec.rb @@ -0,0 +1,88 @@ +describe QA::Factory::Base do + describe '.fabricate!' do + subject { Class.new(described_class) } + let(:factory) { spy('factory') } + let(:product) { spy('product') } + + before do + allow(QA::Factory::Product).to receive(:new).and_return(product) + end + + it 'instantiates the factory and calls factory method' do + expect(subject).to receive(:new).and_return(factory) + + subject.fabricate!('something') + + expect(factory).to have_received(:fabricate!).with('something') + end + + it 'returns fabrication product' do + allow(subject).to receive(:new).and_return(factory) + allow(factory).to receive(:fabricate!).and_return('something') + + result = subject.fabricate!('something') + + expect(result).to eq product + end + + it 'yields factory before calling factory method' do + allow(subject).to receive(:new).and_return(factory) + + subject.fabricate! do |factory| + factory.something! + end + + expect(factory).to have_received(:something!).ordered + expect(factory).to have_received(:fabricate!).ordered + end + end + + describe '.dependency' do + let(:dependency) { spy('dependency') } + + before do + stub_const('Some::MyDependency', dependency) + end + + subject do + Class.new(described_class) do + dependency Some::MyDependency, as: :mydep do |factory| + factory.something! + end + end + end + + it 'appends a new dependency and accessors' do + expect(subject.dependencies).to be_one + end + + it 'defines dependency accessors' do + expect(subject.new).to respond_to :mydep, :mydep= + end + end + + describe 'building dependencies' do + let(:dependency) { double('dependency') } + let(:instance) { spy('instance') } + + subject do + Class.new(described_class) do + dependency Some::MyDependency, as: :mydep + end + end + + before do + stub_const('Some::MyDependency', dependency) + + allow(subject).to receive(:new).and_return(instance) + allow(instance).to receive(:mydep).and_return(nil) + allow(QA::Factory::Product).to receive(:new) + end + + it 'builds all dependencies first' do + expect(dependency).to receive(:fabricate!).once + + subject.fabricate! + end + end +end diff --git a/qa/spec/factory/dependency_spec.rb b/qa/spec/factory/dependency_spec.rb new file mode 100644 index 00000000000..32405415126 --- /dev/null +++ b/qa/spec/factory/dependency_spec.rb @@ -0,0 +1,59 @@ +describe QA::Factory::Dependency do + let(:dependency) { spy('dependency' ) } + let(:factory) { spy('factory') } + let(:block) { spy('block') } + + let(:signature) do + double('signature', factory: dependency, block: block) + end + + subject do + described_class.new(:mydep, factory, signature) + end + + describe '#overridden?' do + it 'returns true if factory has overridden dependency' do + allow(factory).to receive(:mydep).and_return('something') + + expect(subject).to be_overridden + end + + it 'returns false if dependency has not been overridden' do + allow(factory).to receive(:mydep).and_return(nil) + + expect(subject).not_to be_overridden + end + end + + describe '#build!' do + context 'when dependency has been overridden' do + before do + allow(subject).to receive(:overridden?).and_return(true) + end + + it 'does not fabricate dependency' do + subject.build! + + expect(dependency).not_to have_received(:fabricate!) + end + end + + context 'when dependency has not been overridden' do + before do + allow(subject).to receive(:overridden?).and_return(false) + end + + it 'fabricates dependency' do + subject.build! + + expect(dependency).to have_received(:fabricate!) + end + + it 'sets product in the factory' do + subject.build! + + expect(factory).to have_received(:mydep=).with(dependency) + end + end + end +end diff --git a/qa/spec/factory/product_spec.rb b/qa/spec/factory/product_spec.rb new file mode 100644 index 00000000000..3d9e86a641b --- /dev/null +++ b/qa/spec/factory/product_spec.rb @@ -0,0 +1,44 @@ +describe QA::Factory::Product do + let(:factory) { spy('factory') } + let(:product) { spy('product') } + + describe '.populate!' do + it 'instantiates and yields factory' do + expect(described_class).to receive(:new).with(factory) + + described_class.populate!(factory) do |instance| + instance.something = 'string' + end + + expect(factory).to have_received(:something=).with('string') + end + + it 'returns a fabrication product' do + expect(described_class).to receive(:new) + .with(factory).and_return(product) + + result = described_class.populate!(factory) do |instance| + instance.something = 'string' + end + + expect(result).to be product + end + + it 'raises unless block given' do + expect { described_class.populate!(factory) } + .to raise_error ArgumentError + end + end + + describe '.visit!' do + it 'makes it possible to visit fabrication product' do + allow_any_instance_of(described_class) + .to receive(:current_url).and_return('some url') + allow_any_instance_of(described_class) + .to receive(:visit).and_return('visited some url') + + expect(described_class.new(factory).visit!) + .to eq 'visited some url' + end + end +end |