diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2018-10-23 18:23:35 +0800 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2018-10-23 19:17:33 +0800 |
commit | fbf5168b5f34bf1734994e5d85fb9a72edec892d (patch) | |
tree | 595198049af0ee1221bf64f57410404f0d37ae7f | |
parent | 6a0973dc8dcc1a7ff38dc2b045c045437a69ca74 (diff) | |
download | gitlab-ce-51826-single-product-defintion-qa.tar.gz |
Log when both API response and a block are there51826-single-product-defintion-qa
We also explicitly test which value we prefer when
0. Factory attribute
0. API response
0. Block value
Are all provided. We prefer in that order.
-rw-r--r-- | qa/qa/factory/base.rb | 12 | ||||
-rw-r--r-- | qa/qa/runtime/logger.rb | 2 | ||||
-rw-r--r-- | qa/spec/factory/base_spec.rb | 36 |
3 files changed, 46 insertions, 4 deletions
diff --git a/qa/qa/factory/base.rb b/qa/qa/factory/base.rb index b8d1ba96da7..e82e16f9415 100644 --- a/qa/qa/factory/base.rb +++ b/qa/qa/factory/base.rb @@ -33,7 +33,17 @@ module QA end def attribute_value(name, block) - api_resource&.dig(name) || (block && instance_exec(&block)) + api_value = api_resource&.dig(name) + + if api_value && block + log_having_both_api_result_and_block(name, api_value) + end + + api_value || (block && instance_exec(&block)) + end + + def log_having_both_api_result_and_block(name, api_value) + QA::Runtime::Logger.info "<#{self.class}> Attribute #{name.inspect} has both API response `#{api_value}` and a block. API response will be picked. Block will be ignored." end def self.fabricate!(*args, &prepare_block) diff --git a/qa/qa/runtime/logger.rb b/qa/qa/runtime/logger.rb index 3baa24de0ec..83b10e73ad9 100644 --- a/qa/qa/runtime/logger.rb +++ b/qa/qa/runtime/logger.rb @@ -7,7 +7,7 @@ module QA module Logger extend SingleForwardable - def_delegators :logger, :debug, :info, :error, :warn, :fatal, :unknown + def_delegators :logger, :debug, :info, :warn, :error, :fatal, :unknown singleton_class.module_eval do def logger diff --git a/qa/spec/factory/base_spec.rb b/qa/spec/factory/base_spec.rb index 6b12b268633..d7b92052894 100644 --- a/qa/spec/factory/base_spec.rb +++ b/qa/spec/factory/base_spec.rb @@ -154,15 +154,34 @@ describe QA::Factory::Base do end context 'when the product attribute is populated via the api' do + let(:api_resource) { { no_block: 'api' } } + before do - expect(factory).to receive(:api_resource).and_return({ test: 'api' }) + expect(factory).to receive(:api_resource).and_return(api_resource) end it 'returns a fabrication product and defines factory attributes as its methods' do result = subject.fabricate!(factory: factory) expect(result).to be_a(QA::Factory::Product) - expect(result.test).to eq('api') + expect(result.no_block).to eq('api') + end + + context 'when the attribute also has a block in the factory' do + let(:api_resource) { { test: 'api_with_block' } } + + before do + allow(QA::Runtime::Logger).to receive(:info) + end + + it 'returns the api value and emits an INFO log entry' do + result = subject.fabricate!(factory: factory) + + expect(result).to be_a(QA::Factory::Product) + expect(result.test).to eq('api_with_block') + expect(QA::Runtime::Logger) + .to have_received(:info).with(/api_with_block/) + end end end @@ -177,6 +196,19 @@ describe QA::Factory::Base do expect(result).to be_a(QA::Factory::Product) expect(result.test).to eq('value') end + + context 'when the api also has such response' do + before do + allow(factory).to receive(:api_resource).and_return({ test: 'api' }) + end + + it 'returns the factory attribute for the product' do + result = subject.fabricate!(factory: factory) + + expect(result).to be_a(QA::Factory::Product) + expect(result.test).to eq('value') + end + end end context 'when the product attribute has no value' do |