diff options
Diffstat (limited to 'qa/spec/factory/base_spec.rb')
-rw-r--r-- | qa/spec/factory/base_spec.rb | 205 |
1 files changed, 65 insertions, 140 deletions
diff --git a/qa/spec/factory/base_spec.rb b/qa/spec/factory/base_spec.rb index 0457d878270..b9cf60e4cc0 100644 --- a/qa/spec/factory/base_spec.rb +++ b/qa/spec/factory/base_spec.rb @@ -11,124 +11,107 @@ describe QA::Factory::Base do subject { Class.new(described_class) } before do - allow(QA::Factory::Product).to receive(:new).with(product_location).and_return(product) - allow(QA::Factory::Product).to receive(:populate!).and_return(product) allow(subject).to receive(:current_url).and_return(product_location) allow(subject).to receive(:new).and_return(factory) + allow(QA::Factory::Product).to receive(:populate!).with(factory, product_location).and_return(product) end end - shared_examples 'API fabrication method' do |fabrication_method_called, actual_fabrication_method = nil| + shared_examples 'fabrication method' do |fabrication_method_called, actual_fabrication_method = nil| let(:fabrication_method_used) { actual_fabrication_method || fabrication_method_called } - include_context 'fabrication context' + it 'yields factory before calling factory method' do + allow(subject).to receive(:new).and_return(factory) - it_behaves_like 'fabrication method', fabrication_method_called, actual_fabrication_method + expect(factory).to receive(:something!).ordered + expect(factory).to receive(fabrication_method_used).ordered.and_return(product_location) - it 'instantiates the factory and calls factory method' do - expect(subject).to receive(:new).and_return(factory) + subject.public_send(fabrication_method_called) do |factory| + factory.something! + end + end - subject.public_send(fabrication_method_called) + it 'does not log the factory and build method when VERBOSE=false' do + stub_env('VERBOSE', 'false') + expect(factory).to receive(fabrication_method_used).and_return(product_location) - expect(factory).to have_received(fabrication_method_used) + expect { subject.public_send(fabrication_method_called, 'something') } + .not_to output(/Resource #{factory.class.name} built via/) + .to_stdout end + end - it 'returns fabrication product' do - result = subject.public_send(fabrication_method_called) + describe '.fabricate!' do + context 'when factory does not support fabrication via the API' do + before do + expect(described_class).to receive(:fabricate_via_api!).and_raise(NotImplementedError) + end + + it 'calls .fabricate_via_browser_ui!' do + expect(described_class).to receive(:fabricate_via_browser_ui!) - expect(result).to eq product + described_class.fabricate! + end end - it 'logs the factory and build method when VERBOSE=true' do - stub_env('VERBOSE', 'true') + context 'when factory supports fabrication via the API' do + it 'calls .fabricate_via_browser_ui!' do + expect(described_class).to receive(:fabricate_via_api!) - expect { subject.public_send(fabrication_method_called) } - .to output(/Resource #{factory.class.name} built via do_fabricate_via_api/) - .to_stdout + described_class.fabricate! + end end end - shared_examples 'Browser UI fabrication method' do |fabrication_method_called, actual_fabrication_method = nil| - let(:fabrication_method_used) { actual_fabrication_method || fabrication_method_called } - + describe '.fabricate_via_api!' do include_context 'fabrication context' - it_behaves_like 'fabrication method', fabrication_method_called, actual_fabrication_method - - it 'instantiates the factory and calls factory method' do - expect(subject).to receive(:new).and_return(factory) - - subject.public_send(fabrication_method_called, 'something') + it_behaves_like 'fabrication method', :fabricate_via_api! - expect(factory).to have_received(fabrication_method_used).with('something') - end + it 'instantiates the factory, calls factory method returns fabrication product' do + expect(factory).to receive(:fabricate_via_api!).and_return(product_location) - it 'returns fabrication product' do - result = subject.public_send(fabrication_method_called, 'something') + result = subject.fabricate_via_api! - expect(result).to eq product + expect(result).to eq(product) end it 'logs the factory and build method when VERBOSE=true' do stub_env('VERBOSE', 'true') + expect(factory).to receive(:fabricate_via_api!).and_return(product_location) - expect { subject.public_send(fabrication_method_called, 'something') } - .to output(/Resource #{factory.class.name} built via do_fabricate_via_browser_ui/) + expect { subject.fabricate_via_api! } + .to output(/Resource #{factory.class.name} built via api/) .to_stdout end end - shared_examples 'fabrication method' do |fabrication_method_called, actual_fabrication_method = nil| - let(:fabrication_method_used) { actual_fabrication_method || fabrication_method_called } - + describe '.fabricate_via_browser_ui!' do include_context 'fabrication context' - it 'yields factory before calling factory method' do - allow(subject).to receive(:new).and_return(factory) - - subject.public_send(fabrication_method_called) do |factory| - factory.something! - end + it_behaves_like 'fabrication method', :fabricate_via_browser_ui!, :fabricate! - expect(factory).to have_received(:something!).ordered - expect(factory).to have_received(fabrication_method_used).ordered - end - - it 'does not log the factory and build method when VERBOSE=false' do - stub_env('VERBOSE', 'false') + it 'instantiates the factory and calls factory method' do + subject.fabricate_via_browser_ui!('something') - expect { subject.public_send(fabrication_method_called, 'something') } - .not_to output(/Resource #{factory.class.name} built via/) - .to_stdout + expect(factory).to have_received(:fabricate!).with('something') end - end - describe '.fabricate!' do - context 'when factory does not support fabrication via the API' do - before do - allow(factory).to receive(:api_support?).and_return(false) - end + it 'returns fabrication product' do + result = subject.fabricate_via_browser_ui!('something') - it_behaves_like 'Browser UI fabrication method', :fabricate! + expect(result).to eq(product) end - context 'when factory supports fabrication via the API' do - before do - allow(factory).to receive(:api_support?).and_return(true) - end + it 'logs the factory and build method when VERBOSE=true' do + stub_env('VERBOSE', 'true') - it_behaves_like 'API fabrication method', :fabricate!, :fabricate_via_api! + expect { subject.fabricate_via_browser_ui!('something') } + .to output(/Resource #{factory.class.name} built via browser_ui/) + .to_stdout end end - describe '.fabricate_via_api!' do - it_behaves_like 'API fabrication method', :fabricate_via_api! - end - - describe '.fabricate_via_browser_ui!' do - it_behaves_like 'Browser UI fabrication method', :fabricate_via_browser_ui!, :fabricate! - end - describe '.dependency' do let(:dependency) { spy('dependency') } @@ -168,8 +151,7 @@ describe QA::Factory::Base do allow(subject).to receive(:new).and_return(instance) allow(subject).to receive(:current_url).and_return(product_location) allow(instance).to receive(:mydep).and_return(nil) - allow(QA::Factory::Product).to receive(:new) - allow(QA::Factory::Product).to receive(:populate!) + expect(QA::Factory::Product).to receive(:populate!) end it 'builds all dependencies first' do @@ -181,79 +163,22 @@ describe QA::Factory::Base do end describe '.product' do - context 'when the product is produced via the browser' do - subject do - Class.new(described_class) do - def fabricate! - "any" - end - - # Defined only to be stubbed - def self.find_page - end - - product :token do - find_page.do_something_on_page! - 'resulting value' - end - end - end - - it 'appends new product attribute' do - expect(subject.attributes).to be_one - expect(subject.attributes).to have_key(:token) - end - - describe 'populating fabrication product with data' do - let(:page) { spy('page') } + include_context 'fabrication context' - before do - allow(QA::Factory::Product).to receive(:new).and_return(product) - allow(product).to receive(:page).and_return(page) - allow(subject).to receive(:current_url).and_return(product_location) - allow(subject).to receive(:find_page).and_return(page) + subject do + Class.new(described_class) do + def fabricate! + "any" end - it 'populates product after fabrication' do - subject.fabricate! - - expect(product.token).to eq 'resulting value' - expect(page).to have_received(:do_something_on_page!) - end + product :token end end - context 'when the product is producted via the API' do - subject do - Class.new(described_class) do - def fabricate! - "any" - end - - product :token - end - end - - it 'appends new product attribute' do - expect(subject.attributes).to be_one - expect(subject.attributes).to have_key(:token) - end - - describe 'populating fabrication product with data' do - before do - allow(subject).to receive(:new).and_return(factory) - allow(factory).to receive(:class).and_return(subject) - allow(factory).to receive(:api_support?).and_return(true) - allow(factory).to receive(:api_resource).and_return({ token: 'resulting value' }) - allow(QA::Factory::Product).to receive(:new).and_return(product) - end - - it 'populates product after fabrication' do - subject.fabricate! - - expect(product.token).to eq 'resulting value' - end - end + it 'appends new product attribute' do + expect(subject.attributes).to be_one + expect(subject.attributes[0]).to be_a(QA::Factory::Product::Attribute) + expect(subject.attributes[0].name).to eq(:token) end end end |