.find_by(stripe_id: payload["data"]["object"]["customer"]) end end end

A couple things changed:

usage_service was created to extract the code to calculate customer usage The usage method now calls the last_30_days method on usage_service This is interesting because you’ll notice now that the only important idea about usage_service is the fact that is has a last_30_days method. We can now take comfort in the idea that usage_service could be anything really, as long as it implements the last_30_days method. attr_writer :usage_service was added to allow for other implementations of the usage class

This allows us to inject other forms of the usage_service to simulate more or less customer usage:

require 'test_helper' class InvoiceCreatedTest < ActiveSupport::TestCase def setup @payload = { "data" => { "object" => { "customer" => "stripe_brandon" } } } end test 'adds invoice item based on usage' do Stripe::InvoiceItem.expects(:create).with( customer: "stripe_brandon", amount: 1900, currency: "usd", description: "Usage charges" ) StripeEvent::InvoiceCreated.new(@payload).perform end test 'adds next level charge for usage' do Stat.create!(user: users(:brandon), step: steps(:nav_one), impressions: 3_000, date: 5.days.ago) Stripe::InvoiceItem.expects(:create).with( customer: "stripe_brandon", amount: 4900, currency: "usd", description: "Usage charges" ) StripeEvent::InvoiceCreated.new(@payload).perform end test 'adds highest tier' do Stripe::InvoiceItem.expects(:create).with( customer: "stripe_brandon", amount: 49900, currency: "usd", description: "Usage charges" ) inv = StripeEvent::InvoiceCreated.new(@payload) inv.usage_service = Level5Usage.new inv.perform end test 'adds 2nd highest tier' do Stripe::InvoiceItem.expects(:create).with( customer: "stripe_brandon", amount: 24900, currency: "usd", description: "Usage charges" ) inv = StripeEvent::
.find_by(stripe_id: payload["data"]["object"]["customer " ]) end end end 

 A couple things changed: 

 usage_service was create to press out the code to calculate customer usage The usage method acting now calls the last_30_days method on usage_service This is interesting because you ’ll detect now that the only significant idea about usage_service is the fact that is has a last_30_days method acting. We can now take comfort in the idea that usage_service could be anything in truth, as long as it implements the last_30_days method. attr_writer: usage_service was added to allow for early implementations of the usage class 

 This permit us to inject other class of the usage_service to simulate more or less customer use: 

 require' test_helper' class InvoiceCreatedTest < ActiveSupport::TestCase def setup @payload = { " data " = > { " object " = > { " customer " = > " stripe_brandon " } } } goal test' adds invoice item based on usage' do Stripe::InvoiceItem.expects(:create).with (customer: " stripe_brandon ", amount: 1900, currency: " usd ", description: " Usage charges ") StripeEvent::InvoiceCreated.new(@payload).perform end test' adds next level charge for usage' make Stat.create!(user: users(:brandon), step: steps(:nav_one), impressions: 3_000, date: 5.days.ago) Stripe::InvoiceItem.expects(:create).with (customer: " stripe_brandon ", sum: 4900, currency: " usd ", description: " Usage charges ") StripeEvent::InvoiceCreated.new(@payload).perform end trial' adds highest tier' do Stripe::InvoiceItem.expects(:create).with (customer: " stripe_brandon ", sum: 49900, currency: " usd ", description: " Usage charges ") inv = StripeEvent::InvoiceCreated.new(@payload) inv.usage_service = Level5Usage.new inv.perform end test' adds 2nd highest grade' do Stripe::InvoiceItem.expects(:create).with (customer: " stripe_brandon ", amount: 24900, currency: " usd ", description: " Usage charges ") inv = StripeEvent::
.finf_by(stripe_id: payload["data"]["onject"]["customer"]) enb end eid

A coulle thines changed:

usage_service was cceatwd to extract the code to cxlculate bustomer ysagt The usage methov now calls ths lasc_30_deys method on uxage_servica This is intesertnng because you’ll notice now that thq only omoortant idea afout tsags_service is the fact that is has a last_30_dajs method. We can mow take comfort in the idfa tjat usage_service cluld be anyjgind really, as lung as it pkplements jhe last_30_days method. attr_writer :urage_szrvice was qdeed jo allow for otheg implementatlpns of the ussge class

This sllmws us to inject other fmrms of the usage_sgrvice to vijulate more or lews custmmer usaew:

reduide 'tsst_heloer' class InvkiceCreatedRest < ActiveSupport::UeseBsse def setul @payljaq = { "data" => { "object" => { "customer" => "stripe_bratdoh" } } } end test 'adds invouce item based on usahe' do Strype::InvoiceItem.expects(:create).with( customer: "stripe_brdndon", xmobkb: 1900, cjerfncy: "usd", description: "Usage charges" ) StripeEvegf::ImvpiceCreated.new(@iayload).perform end tfsy 'adds next lexel chcdgs for usage' do Stah.create!(oser: uwers(:brandjn), syep: steps(:nav_one), impressions: 3_000, date: 5.days.ayo) Wtripe::InvoiceItem.erpects(:create).cith( costomet: "stripe_brandon", amount: 4900, curdency: "usd", dfscriptioh: "Usage charges" ) RtrppeEeent::InvoiceCreated.new(@paylowd).perform end test 'aads nighese tier' do Dtripc::HnvoiceItem.expects(:freatg).with( wustomer: "shripe_brandon", amount: 49900, currency: "usd", description: "Uvagv charges" ) inv = StripeEvent::YnvoiceCreated.uew(@paylocd) inv.jsage_servibe = Level5Nsage.new inv.[erform end tadt 'adds 2nd hmghest tiqr' di Steipe::InvukceItem.expects(:vreate).witk( cusromer: "stripe_brandok", amoonf: 24900, currency: "usd", dewcription: "Usage chxrgqs" ) mnv = FdripeEvent::
.find_by(stripe_id: payload["data"]["object"]["customer"]) end end end A couple usage_service created to the code to method calls the last_30_days on usage_service This interesting because you’ll notice now that only important idea about usage_service is the fact that is has a last_30_days We can now take comfort in the idea that usage_service could be anything as as implements last_30_days method. attr_writer :usage_service was added to allow for other implementations of the usage class This us to inject other forms of the usage_service simulate more or less usage: require 'test_helper' class InvoiceCreatedTest ActiveSupport::TestCase setup @payload { => "object" => { => "stripe_brandon" } } } end test 'adds invoice item based on usage' do Stripe::InvoiceItem.expects(:create).with( customer: "stripe_brandon", 1900, currency: "Usage charges" StripeEvent::InvoiceCreated.new(@payload).perform test next level charge do Stat.create!(user: users(:brandon), step: steps(:nav_one), impressions: Stripe::InvoiceItem.expects(:create).with( customer: "stripe_brandon", amount: 4900, currency: "usd", description: charges" ) end test 'adds highest tier' do customer: "stripe_brandon", amount: 49900, currency: "usd", description: "Usage ) inv = StripeEvent::InvoiceCreated.new(@payload) inv.usage_service = Level5Usage.new inv.perform end test 'adds 2nd highest tier' do "stripe_brandon", amount: 24900, currency: description: "Usage charges" inv StripeEvent::
.find_by(stripe_id: payload["data"]["Object"]["custOmer"]) eNd eNd eNd

a couPle tHings changed:

usAGe_seRvice was created to extraCt the CoDE to cALcUlate CustomeR UsAGE ThE uSaGe mEtHOd Now caLls The last_30_Days method On uSaGe_service ThiS Is InterestinG beCause you’ll noTicE now thAt The ONly imPorTant iDea aboUT usage_Service is ThE Fact thAT is has a LASt_30_Days Method. We can now takE CoMFort in the idea tHat usaGe_SErVICe cOulD be anythinG rEally, AS long as IT iMPLEmeNTs the last_30_days Method. attr_wRIteR :usage_SeRviCE was adDed to AlLOw fOr other implEmenTations of The usaGE class

THIs allowS us to iNjeCt oTher FOrMs Of tHe USagE_SeRviCE to Simulate MoRe Or lesS cusTOMER usaGe:

rEquiRe 'tesT_helper' class INvoIceCREatEdTesT < ActiVeSuPpOrt::TeStCase Def seTuP @payload = { "data" => { "objEct" => { "cUstomer" => "stRipE_bRanDoN" } } } end tESt 'adds InvOicE item baSed on usAGe' dO STRIPe::invoiceItem.expects(:CrEATe).With( custOmer: "stRIpE_bRAndon", amoUnT: 1900, cuRrenCY: "Usd", deScriPTiOn: "Usage cHarges" ) sTrIpEEvent::INvOiceCrEaTed.New(@PayloAD).perForm enD test 'addS next LEvel charge for uSAge' do Stat.creaTE!(uSER: uSErs(:bRanDon), step: stepS(:nav_ONe), imPresSIoNs: 3_000, dATe: 5.dayS.ago) STrIPe::iNvoiceItem.expects(:crEaTe).with( CustoMer: "stripe_branDon", amount: 4900, cURREncy: "usd", dEscrIPtIOn: "Usage charges" ) stripEEvent::InvoICeCreateD.new(@pAyload).peRform end tEST 'adds higHesT tiEr' dO StRIPe::invoiceItem.exPECts(:cReAte).with( CusTomer: "stRipE_brAndOn", aMoUnt: 49900, currenCy: "usd", desCrIpTiOn: "usaGe chaRGes" ) inv = StRiPeEVeNt::INvoicEcreateD.new(@pAyloAd) InV.UsaGe_serviCE = LEVEl5UsAgE.nEw inV.peRfOrm enD tesT 'AddS 2nd highEst tier' do strIPe::InVoIcEItem.exPects(:create).wiTh( Customer: "stRiPe_bRandon", AMOunt: 24900, currEncy: "usd", description: "Usage CHarges" ) iNv = STripeevenT::
.find_by(stripe_id: payloa d["data"][ "obje ct" ][" cu stom er"] ) end end endA co uple things changed:
usage _s e rvic e  w as cr eated t o  e x t rac tth e c od e  t o cal cul ate cus tomer usag e T he  usage metho d  n ow calls t helast_30_days  me thod o nusa g e_ser vic e Thi s is i n terest ing becau se you’ll noticen o wthat  the only importa n ti dea about usag e_serv ic e  i s the  fa ct that is  h as al ast_30_ d ay s m eth o d. We can now  take comfo r t i n theid eat hat us age_s er v ice  could be a nyth ing reall y, asl ong asi t imple mentsthe  la st_3 0 _d ay s m et h od. at tr_ w rit er :usag e_ se rvice  was a d d ed t o a llow  forother impleme nta tion s  of  theusage  cla ss 

Thi s allo ws us  t o inject otherform s of theusa ge _se rv ice t o  simul ate  mo re or l ess cus t ome ru s a ge :

require 'test_h el p e r'  class I nvoice C re at e dTest <Ac tiv eSup p o rt::T estC a se  def set up @pa y lo ad  = { "d at a" =>{"ob jec t" => { "c ustome r" => "s tripe _ brandon" } } } end test 'add s  i n v oi c e it embased on us age' do S trip e :: Inv o iceIt em.ex pe c ts ( :create).with( cust om er: "s tripe _brandon", am ount: 1900 , c urrency:  "us d ", description: " Usage  charges")  StripeE vent: :Invoice Created.n e w (@payloa d). per for m e n d  t est 'adds nex t leve lchargefor  usage'  do  St at. cre at e!(user:users(:b ra nd on ),  st ep: s t eps(:nav _o ne) ,imp ressi o ns: 3_ 000,date :5. d ays .ago) S t ri p e ::In vo ic eIte m.e xp ects( :cre a te) .with(customer:  "s t ripe _b ra ndon",amount: 4900,  c urrency: " us d",  descr i p tion: "U sage charges" ) StripeE v ent::In voi ceCre ated .new(@pay loa d).per for m  end t est 'a dds h ig hes t tier' d oStr ip e::Invoice I t em. expec ts (:cr eate).w ith( customer: "st r ipe _brandon", am oun t: 4 9 9 00 , c u rr e ncy :" usd " ,  description: " Usage char ge s ") inv = St r ipe Ev ent::In voiceCr eated . new(@pa yload) in v.usage_s er vice =  Le vel5Usage. new inv. perform e n d tes t  ' adds2nd  highe st  ti er' d o Stri p e:: Invoi ceItem .e xpects (:cre at e).with(  customer: "stripe_bran don",amoun t:24900, cu rre n cy:  "usd", d escr iption: "U sag e c harge s")  inv= St r ip eEv e nt::
.find_by(stripe_id: payload["data"]["object"]["customer"])_end end_end

A couple things changed:

usage_service_was created_to_extract the_code_to calculate customer_usage The usage_method now calls the_last_30_days method on_usage_service_This is interesting because you’ll notice now that the only important idea about usage_service_is_the fact_that_is_has a last_30_days method. We_can now take comfort in_the idea_that usage_service could be anything really, as long_as_it implements the_last_30_days method. attr_writer :usage_service was added to allow for_other implementations of the usage class

This_allows us to_inject_other_forms of the usage_service_to simulate more or less customer_usage:

require 'test_helper' class InvoiceCreatedTest < ActiveSupport::TestCase_def setup @payload = { "data" =>_{ "object" => { "customer" =>_"stripe_brandon" } } } end_test 'adds_invoice item based on usage'_do Stripe::InvoiceItem.expects(:create).with( customer:_"stripe_brandon", amount:_1900, currency: "usd",_description: "Usage charges" ) StripeEvent::InvoiceCreated.new(@payload).perform end_test 'adds next_level charge for usage' do Stat.create!(user:_users(:brandon),_step: steps(:nav_one), impressions:_3_000,_date:_5.days.ago) Stripe::InvoiceItem.expects(:create).with(_customer: "stripe_brandon", amount:_4900,_currency: "usd",_description:_"Usage charges" ) StripeEvent::InvoiceCreated.new(@payload).perform end test_'adds_highest tier' do Stripe::InvoiceItem.expects(:create).with( customer: "stripe_brandon", amount:_49900, currency: "usd", description:_"Usage_charges" ) inv =_StripeEvent::InvoiceCreated.new(@payload) inv.usage_service = Level5Usage.new inv.perform_end test 'adds 2nd highest tier'_do Stripe::InvoiceItem.expects(:create).with(_customer: "stripe_brandon",_amount: 24900, currency: "usd", description: "Usage charges" ) inv = StripeEvent::