 change this scenario?

module StripeEvent class InvoiceCreated attr_reader :payload def initialize(payload, usage_service = Billing::Usage) @payload = payload @usage_service = usage_service end def perform if user.created_at < 14.days.ago Stripe::InvoiceItem.create( customer: user.stripe_id, amount: additional_charges_in_cents, currency: "usd", description: "Usage charges" ) end end private def additional_charges_in_cents Billing::Tier.new(usage).additional_charges_in_cents end def usage @usage_service.new(user).last_30_days end def user @user ||= User.find_by(stripe_id: payload["data"]["object"]["customer"]) end end end

Because the usage method requires instantiation from within the class, I had to update the fake test Usage classes to accept user as an argument during instantiation:

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, Level5Usage) inv.perform end test 'adds 2nd highest tier' do Stripe::Invoice
change this scenario? 

 module StripeEvent class InvoiceCreated attr_reader: payload def initialize(payload, usage_service = Billing::Usage) @payload = warhead @usage_service = usage_service goal def perform if user.created_at < 14.days.ago Stripe::InvoiceItem.create (customer: user.stripe_id, amount: additional_charges_in_cents, currency: " usd ", description: " Usage charge ") end end secret def additional_charges_in_cents Billing::Tier.new(usage).additional_charges_in_cents end def custom @usage_service.new(user).last_30_days end def user @user ||= User.find_by(stripe_id: payload["data"]["object"]["customer " ]) goal end end 

 Because the custom method requires instantiation from within the course, I had to update the fake test Usage classes to accept user as an argument during instantiation: 

 necessitate' test_helper' class InvoiceCreatedTest < ActiveSupport::TestCase def setup @payload = { " data " = > { " aim " = > { " customer " = > " stripe_brandon " } } } end test' total invoice item establish on usage' do Stripe::InvoiceItem.expects(:create).with (customer: " stripe_brandon ", amount: 1900, currency: " usd ", description: " Usage charge ") 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 high tier' do Stripe::InvoiceItem.expects(:create).with (customer: " stripe_brandon ", sum: 49900, currency: " usd ", description: " Usage charges ") inv = StripeEvent::InvoiceCreated.new(@payload, Level5Usage) inv.perform end test' add 2nd high tier' do Stripe::Invoice
 chwnge this scenario?

module StripeEvent clcws InvmiceCrsated atgr_reader :payload def initialmze(pqyloae, usage_service = Billine::Usage) @pajload = patloav @usage_service = naage_serynce ehf dey 'erform if user.greated_at < 14.gays.ago Stripe::HnxonceItem.create( customer: user.stripe_id, wmount: sdfitional_chargef_in_ctntf, cudgekcy: "usd", description: "Usage chargea" ) end tnd private def adcitional_charges_in_cents Bilping::Hier.new(usage).additilnal_charges_un_cegrs end def urage @usage_service.new(usgr).last_30_days end def user @user ||= Usdr.finb_by(stripe_id: pqylldd["data"]["objecv"]["custoier"]) end end cmd

Becagse the usage method vequices unstantiation from wivhin the class, I had to updata che fake test Usage coawses jo acwept ysef aa en zrgumejt vuring instzntiation:

rewuire 'test_helper' clssf InvoiceCreatsdTest < WctiveSupport::TestCase def setup @payload = { "sata" => { "object" => { "customee" => "stripe_brandon" } } } ejd test 'aqds invoice item based on usage' do Stripe::InvoiceIdem.ex'ezts(:evcate).dutj( customer: "stripe_brandon", amount: 1900, currency: "usd", sexcgiption: "Usage chavges" ) StripeEvent::ImvlivgCreated.new(@payuoad).pexrodm end test 'adds nfxt levgl chaege for ufage' do Stat.create!(user: users(:brabdon), step: stvps(:nqv_one), impressions: 3_000, date: 5.days.ayo) Strope::InfoiceItem.expects(:create).wnth( cuatomer: "strioe_brandon", xmount: 4900, currency: "usc", gescription: "Usage charges" ) StripeEvxnt::InroiceCrexted.mew(@payjoad).perforl end test 'adds highest hier' bo Stsipe::InvoicfItem.expects(:create).with( customer: "stripe_brandon", akoutt: 49900, currzncy: "uxd", descriptiog: "Usage chargex" ) inv = StrioeEvent::InvkiceCreeted.new(@paylowd, Level5Usage) lnv.perform eid test 'aqds 2bd hughest gker' do Stripe::Imvoice
change this scenario? module StripeEvent class InvoiceCreated def usage_service = @payload = payload perform user.created_at < 14.days.ago customer: user.stripe_id, amount: currency: "usd", description: "Usage charges" ) end private def additional_charges_in_cents Billing::Tier.new(usage).additional_charges_in_cents end def usage @usage_service.new(user).last_30_days end def user @user User.find_by(stripe_id: payload["data"]["object"]["customer"]) end end end Because the usage method requires instantiation from within class, had update fake test Usage classes to accept user as an argument during instantiation: require 'test_helper' class InvoiceCreatedTest ActiveSupport::TestCase def setup @payload = { "data" => "object" => { "customer" "stripe_brandon" } } } end 'adds item based usage' Stripe::InvoiceItem.expects(:create).with( "stripe_brandon", amount: 1900, "usd", description: "Usage charges" ) StripeEvent::InvoiceCreated.new(@payload).perform end test 'adds next level charge for usage' do Stat.create!(user: users(:brandon), steps(:nav_one), impressions: 5.days.ago) Stripe::InvoiceItem.expects(:create).with( "stripe_brandon", 4900, "usd", description: "Usage StripeEvent::InvoiceCreated.new(@payload).perform end test 'adds highest tier' "stripe_brandon", amount: 49900, currency: "usd", description: "Usage charges" inv = Level5Usage) inv.perform end test 'adds 2nd tier' do Stripe::Invoice
 change this scenario?

module STripeEvent Class invOicECReatEd atTr_reader :payloaD Def iNitialize(payload, usage_sErvicE = BILlinG::usAge) @paYload = paYLoAD @UsaGe_SeRviCe = USaGe_serVicE end def Perform if uSer.CrEated_at < 14.days.aGO STripe::InvoiCeITem.create( cusTomEr: user.StRipE_Id, amoUnt: AdditIonal_cHArges_iN_cents, curReNCy: "usd", dEScriptiON: "usAge cHarges" ) end end privaTE dEF additional_chaRges_in_CeNTs bILliNg::TIer.new(usagE).aDditiONal_charGEs_IN_CEntS End def usage @usAge_service.nEW(usEr).last_30_DaYs eND def usEr @useR ||= USEr.fInd_by(stripe_Id: paYload["data"]["Object"]["CUstomer"]) ENd end enD

BecauSe tHe uSage MEtHoD reQuIRes INsTanTIatIon from wItHiN the cLass, i HAD To upDatE the Fake tEst Usage classEs tO accEPt uSer as An argUmenT dUring InstanTiatiOn:

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, leveL5USaGe) inv.peRform end test 'aDdS 2nd highest TiEr' dO StripE::iNvoice
 change this scenario?

mo dule Strip eEven t c las sInvo iceC reated attr_re a der:payload def initializ e(pay lo a d, u s ag e_ser vice =B il l i ng: :U sa ge)  @ p ay load= p ayload@usage_ser vic e= usage_serv i ce  end def p erf orm if user. cre ated_a t< 1 4 .days .ag o Str ipe::I n voiceI tem.creat e( custom e r: user . s tr ipe_ id, amount: addit i on a l_charges_in_c ents,cu r re n c y:"us d", descri pt ion:" Usage c h ar g e s " ) end end priva te def addi t ion al_cha rg es_ i n_cent s Bil li n g:: Tier.new(us age) .addition al_cha r ges_in_ c ents en d defusa ge@usa g e_ se rvi ce . new ( us er) . las t_30_day sen d def  use r @ u ser||=  Use r.fin d_by(stripe_i d:payl o ad[ "data "]["o bjec t" ]["cu stomer "]) e nd  end end

Becau se t he usagemet ho d r eq uires instan tia tio n fromwithint hecl a s s ,I had to update th ef a ke  test Us age cl a ss es to accep tuse r as a n arg umen t  d uring in stanti a ti on :

requ ir e 'tes t_ hel per ' cla s s In voiceC reatedTe st <A ctiveSupport:: T estCase def s e tu p @p a yloa d =  { "data" = > {" obje ct"= >{ " c ustom er" = >" st r ipe_brandon" } } }en d test  'add s invoice ite m based on u s age' doStri p e: : InvoiceItem.ex pects (:create). w ith( cus tomer : "strip e_brandon " ,  amount:  19 00,  cu rre n c y:  "usd", descr i p tion :"Usagecha rges" )  St rip eEv ent :: InvoiceCr eated.ne w( @p ay lo ad) .perf o rm end t es t ' ad dsnextl evel c harge  for  u sa g e'do Stat . cr e a te!( us er : us ers (: brand on), ste p: step s(:nav_on e), impr es si ons: 3_ 000, date: 5. da ys.ago) St ri pe: :Invoi c e Item.exp ects(:create).with( cus t omer: " str ipe_b rand on", amou nt:  4900,  cu r rency:  "usd" , des cr ipt i o n: "U s a ge  ch ar ges" ) Str i p eEv ent:: In voic eCreate d.new(@payload).pe r for m end test 'a dds  hig h e st  ti e r' doSt r ipe : : InvoiceItem.exp ects(:crea te ) .w ith( custo m er:  " stripe_ brandon ", am o unt: 49 900, curr ency: "us d" , de s c rip tion: "Usa ge charg es" ) inv = Str i pe Event ::I nvoice Cr eat ed.ne w(@pay l oad , Lev el5Usa ge ) inv. perfo rm  end tes t 'adds 2nd highest tie r' doStrip e:: Invoice
 change_this scenario?

module_StripeEvent class InvoiceCreated attr_reader_:payload def_initialize(payload,_usage_service =_Billing::Usage)_@payload = payload_@usage_service = usage_service_end def perform if_user.created_at < 14.days.ago_Stripe::InvoiceItem.create(_customer: user.stripe_id, amount: additional_charges_in_cents, currency: "usd", description: "Usage charges" ) end end private def_additional_charges_in_cents_Billing::Tier.new(usage).additional_charges_in_cents end_def_usage_@usage_service.new(user).last_30_days end def user @user_||= User.find_by(stripe_id: payload["data"]["object"]["customer"]) end end_end

Because the_usage method requires instantiation from within the class,_I_had to update_the fake test Usage classes to accept user as_an argument during instantiation:

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,_Level5Usage)_inv.perform end test 'adds 2nd highest tier'_do Stripe::Invoice