diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2018-10-25 23:29:24 +0300 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2018-10-25 23:29:24 +0300 |
commit | 51518019645ad514b98951df0bd30ecb898786aa (patch) | |
tree | 65fdeda5aa21db221ee296a87fa9ed7f4fc9f68c /qa/spec | |
parent | bf96ec85c736747672fa550707829fef635ddd60 (diff) |
Always use `attribute` to define the product
Diffstat (limited to 'qa/spec')
-rw-r--r-- | qa/spec/factory/base_spec.rb | 143 | ||||
-rw-r--r-- | qa/spec/factory/dependency_spec.rb | 79 | ||||
-rw-r--r-- | qa/spec/factory/product_spec.rb | 66 |
3 files changed, 109 insertions, 179 deletions
diff --git a/qa/spec/factory/base_spec.rb b/qa/spec/factory/base_spec.rb index 229f93a1041..d7b92052894 100644 --- a/qa/spec/factory/base_spec.rb +++ b/qa/spec/factory/base_spec.rb @@ -19,7 +19,7 @@ describe QA::Factory::Base do before do 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) + allow(QA::Factory::Product).to receive(:new).with(factory).and_return(product) end end @@ -115,73 +115,134 @@ describe QA::Factory::Base do end end - describe '.dependency' do - let(:dependency) { spy('dependency') } + shared_context 'simple factory' do + subject do + Class.new(QA::Factory::Base) do + attribute :test do + 'block' + end - before do - stub_const('Some::MyDependency', dependency) - end + attribute :no_block - subject do - Class.new(described_class) do - dependency Some::MyDependency, as: :mydep do |factory| - factory.something! + def fabricate! + 'any' + end + + def self.current_url + 'http://stub' end end end - it 'appends a new dependency and accessors' do - expect(subject.dependencies).to be_one + let(:factory) { subject.new } + end + + describe '.attribute' do + include_context 'simple factory' + + it 'appends new product attribute' do + expect(subject.attributes_names).to eq([:no_block, :test, :web_url]) end - it 'defines dependency accessors' do - expect(subject.new).to respond_to :mydep, :mydep= + context 'when the product attribute is populated via a block' do + 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('block') + end end - describe 'dependencies fabrication' do - let(:dependency) { double('dependency') } - let(:instance) { spy('instance') } + 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(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.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) - subject do - Class.new(described_class) do - dependency Some::MyDependency, as: :mydep + 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 + context 'when the product attribute is populated via a factory attribute' do before do - stub_const('Some::MyDependency', dependency) + factory.test = 'value' + 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('value') + end - 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) - expect(QA::Factory::Product).to receive(:populate!) + 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 - it 'builds all dependencies first' do - expect(dependency).to receive(:fabricate!).once + context 'when the product attribute has no value' do + it 'raises an error because no values could be found' do + result = subject.fabricate!(factory: factory) - subject.fabricate! + expect { result.no_block } + .to raise_error(described_class::NoValueError, "No value was computed for product no_block of factory #{factory.class.name}.") end end end - describe '.product' do - include_context 'fabrication context' + describe '#web_url' do + include_context 'simple factory' - subject do - Class.new(described_class) do - def fabricate! - "any" - end + it 'sets #web_url to #current_url after fabrication' do + subject.fabricate!(factory: factory) - product :token - end + expect(factory.web_url).to eq(subject.current_url) + end + end + + describe '#visit!' do + include_context 'simple factory' + + before do + allow(factory).to receive(:visit) 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) + it 'calls #visit with the underlying #web_url' do + factory.web_url = subject.current_url + factory.visit! + + expect(factory).to have_received(:visit).with(subject.current_url) end end end diff --git a/qa/spec/factory/dependency_spec.rb b/qa/spec/factory/dependency_spec.rb deleted file mode 100644 index 657beddffb1..00000000000 --- a/qa/spec/factory/dependency_spec.rb +++ /dev/null @@ -1,79 +0,0 @@ -describe QA::Factory::Dependency do - let(:dependency) { spy('dependency' ) } - let(:factory) { spy('factory') } - let(:block) { spy('block') } - - let(:signature) do - double('signature', name: :mydep, factory: dependency, block: block) - end - - subject do - described_class.new(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 - - it 'calls given block with dependency factory and caller factory' do - expect(dependency).to receive(:fabricate!).and_yield(dependency) - - subject.build! - - expect(block).to have_received(:call).with(dependency, factory) - end - - context 'with no block given' do - let(:signature) do - double('signature', name: :mydep, factory: dependency, block: nil) - end - - it 'does not error' do - subject.build! - - expect(dependency).to have_received(:fabricate!) - end - end - end - end -end diff --git a/qa/spec/factory/product_spec.rb b/qa/spec/factory/product_spec.rb index 43b1d93d769..5b6eaa13e9c 100644 --- a/qa/spec/factory/product_spec.rb +++ b/qa/spec/factory/product_spec.rb @@ -1,73 +1,21 @@ describe QA::Factory::Product do let(:factory) do Class.new(QA::Factory::Base) do - def foo - 'bar' + attribute :test do + 'block' end + + attribute :no_block end.new end let(:product) { spy('product') } let(:product_location) { 'http://product_location' } - subject { described_class.new(factory, product_location) } - - describe '.populate!' do - before do - expect(factory.class).to receive(:attributes).and_return(attributes) - end - - context 'when the product attribute is populated via a block' do - let(:attributes) do - [QA::Factory::Product::Attribute.new(:test, proc { 'returned' })] - end - - it 'returns a fabrication product and defines factory attributes as its methods' do - result = described_class.populate!(factory, product_location) - - expect(result).to be_a(described_class) - expect(result.test).to eq('returned') - end - end - - context 'when the product attribute is populated via the api' do - let(:attributes) do - [QA::Factory::Product::Attribute.new(:test)] - end - - it 'returns a fabrication product and defines factory attributes as its methods' do - expect(factory).to receive(:api_resource).and_return({ test: 'returned' }) - - result = described_class.populate!(factory, product_location) + subject { described_class.new(factory) } - expect(result).to be_a(described_class) - expect(result.test).to eq('returned') - end - end - - context 'when the product attribute is populated via a factory attribute' do - let(:attributes) do - [QA::Factory::Product::Attribute.new(:foo)] - end - - it 'returns a fabrication product and defines factory attributes as its methods' do - result = described_class.populate!(factory, product_location) - - expect(result).to be_a(described_class) - expect(result.foo).to eq('bar') - end - end - - context 'when the product attribute has no value' do - let(:attributes) do - [QA::Factory::Product::Attribute.new(:bar)] - end - - it 'returns a fabrication product and defines factory attributes as its methods' do - expect { described_class.populate!(factory, product_location) } - .to raise_error(described_class::NoValueError, "No value was computed for product bar of factory #{factory.class.name}.") - end - end + before do + factory.web_url = product_location end describe '.visit!' do |