2017-12-21 04:38:06 -05:00
|
|
|
describe QA::Factory::Base do
|
2018-01-11 06:00:22 -05:00
|
|
|
let(:factory) { spy('factory') }
|
|
|
|
let(:product) { spy('product') }
|
|
|
|
|
2017-12-21 04:38:06 -05:00
|
|
|
describe '.fabricate!' do
|
|
|
|
subject { Class.new(described_class) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(QA::Factory::Product).to receive(:new).and_return(product)
|
2018-01-26 14:25:01 -05:00
|
|
|
allow(QA::Factory::Product).to receive(:populate!).and_return(product)
|
2017-12-21 04:38:06 -05:00
|
|
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-01-11 04:58:56 -05:00
|
|
|
describe 'dependencies fabrication' do
|
|
|
|
let(:dependency) { double('dependency') }
|
|
|
|
let(:instance) { spy('instance') }
|
2017-12-21 04:38:06 -05:00
|
|
|
|
2018-01-11 04:58:56 -05:00
|
|
|
subject do
|
|
|
|
Class.new(described_class) do
|
|
|
|
dependency Some::MyDependency, as: :mydep
|
|
|
|
end
|
2017-12-21 04:38:06 -05:00
|
|
|
end
|
|
|
|
|
2018-01-11 04:58:56 -05:00
|
|
|
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)
|
2018-01-26 14:25:01 -05:00
|
|
|
allow(QA::Factory::Product).to receive(:populate!)
|
2018-01-11 04:58:56 -05:00
|
|
|
end
|
2017-12-21 04:38:06 -05:00
|
|
|
|
2018-01-11 04:58:56 -05:00
|
|
|
it 'builds all dependencies first' do
|
|
|
|
expect(dependency).to receive(:fabricate!).once
|
|
|
|
|
|
|
|
subject.fabricate!
|
|
|
|
end
|
2017-12-21 04:38:06 -05:00
|
|
|
end
|
2018-01-11 04:58:56 -05:00
|
|
|
end
|
2017-12-21 04:38:06 -05:00
|
|
|
|
2018-01-11 04:58:56 -05:00
|
|
|
describe '.product' do
|
|
|
|
subject do
|
|
|
|
Class.new(described_class) do
|
2018-01-26 14:25:01 -05:00
|
|
|
def fabricate!
|
|
|
|
"any"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Defined only to be stubbed
|
|
|
|
def self.find_page
|
|
|
|
end
|
|
|
|
|
2018-01-11 06:00:22 -05:00
|
|
|
product :token do
|
2018-01-26 14:25:01 -05:00
|
|
|
find_page.do_something_on_page!
|
2018-01-11 06:00:22 -05:00
|
|
|
'resulting value'
|
2018-01-11 04:58:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-12-21 04:38:06 -05:00
|
|
|
|
2018-01-11 04:58:56 -05:00
|
|
|
it 'appends new product attribute' do
|
|
|
|
expect(subject.attributes).to be_one
|
2018-01-11 06:00:22 -05:00
|
|
|
expect(subject.attributes).to have_key(:token)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'populating fabrication product with data' do
|
|
|
|
let(:page) { spy('page') }
|
|
|
|
|
|
|
|
before do
|
2018-01-26 14:25:01 -05:00
|
|
|
allow(factory).to receive(:class).and_return(subject)
|
2018-01-11 06:00:22 -05:00
|
|
|
allow(QA::Factory::Product).to receive(:new).and_return(product)
|
|
|
|
allow(product).to receive(:page).and_return(page)
|
2018-01-26 14:25:01 -05:00
|
|
|
allow(subject).to receive(:find_page).and_return(page)
|
2018-01-11 06:00:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'populates product after fabrication' do
|
|
|
|
subject.fabricate!
|
|
|
|
|
|
|
|
expect(product.token).to eq 'resulting value'
|
2018-01-26 14:25:01 -05:00
|
|
|
expect(page).to have_received(:do_something_on_page!)
|
2018-01-11 06:00:22 -05:00
|
|
|
end
|
2017-12-21 04:38:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|