This very-light skeleton demonstrates on way to connect a merchant to their items. Things to notice:
repository_integration_test.rb
has the one test. Run it withruby test/repository_integration_test.rb
- It creates an instance of
SalesEngine
. Look at thesales_engine.rb
to see that the newSalesEngine
initializes both aMerchantRepository
and aItemRepository
, but most importantly that it passes those objectsself
which is a reference to theSalesEngine
instance. - The initializers of both
MerchantRepository
andItemRepository
store that reference asengine
so they can later reach back up to the SalesEngine they came from - When
MerchantRepository
createsMerchant
instances (line 9 in themerchants
method), it again passesself
in as a parameter. Thatself
is theMerchantRepository
instance. - The test calls the
.items
method on aMerchant
instance. Inmerchant.rb
see how theitems
method reaches up to therepository
(the parent instance ofMerchantRepository
) and asks for theitems_for
and passes itself up to the repo. - In
MerchantRepository#items_for
, the repo reaches up to theengine
and calls theitems_for_merchant_id
and passes in the ID number of themerchant
parameter - In
SalesEngine
, theitems_for_merchant_id
method just callsfind_all_by_merchant_id
on theitem_repository
instance it instantiated in the initialize ItemRepository#find_all_by_merchant_id
actually does the select, and passes the results back to theSalesEngine
, which passes them back to theMerchantRepository
, which passes them back to theMerchant
instance, which passes them back to the test.- The test assertion succeeds, having found three items.
No problem, right? :)