ruby on rails - Q&A. Trailbalzer Testing - Implications of contract method -
the following q&a based on examples given in trailblazer book pp. ~50-60 adapted specific requirements. can think of arinvoice , ar_invoice thing , thing general drift if following in book.
my operation.rb file was:
class arinvoice < gltransaction class create < trailblazer::operation include( model ) model( arinvoice, :create ) contract() property( :invoice_number ) property( :currency_code ) property( :forex_rate ) property( :gl_account_id ) validates( :invoice_number, :presence => true ) validates( :currency_code, :presence => true ) end def process( params ) @model = arinvoice.new # have use instance variable here validate( params[ :ar_invoice ], @model ) |f| f.save end end end end i adapted test trailblazer book:
it("inserts valid invoice") test_time = time.now puts(test_time) ar_invoice = arinvoice::create.( :ar_invoice => { :invoice_number => 101, :gl_account_id => 1, :effective_from => test_time.to_s } ).model ar_invoice.persisted?.must_equal(true) ar_invoice.invoice_number.must_equal(101) ar_invoice.transaction_type.must_equal('in') ar_invoice.effective_from.must_equal(test_time) ar_invoice.superseded_after.must_equal(nil) end and got error:
ar_invoice crud create#test_0001_inserts valid invoice: \ activerecord::statementinvalid: sqlite3::constraintexception: \ gl_transactions.effective_from may not null: \ insert "gl_transactions" . . . but see this:
# running: 2016-02-08 11:24:35 -0500 e so, test_time value set. why not getting effective_from attribute?
the answer give below.
the problem did not grasp significance of contract do/end block used in trailblazer (tbr). way read book contract concerned form. experience ror led me map word form word view. mistake. 1 might trip else well.
the correct contract block contains missing attribute: effective_from
contract() property( :invoice_number ) property( :currency_code ) property( :forex_rate ) property( :gl_account_id ) property( :effective_from ) validates( :invoice_number, :presence => true ) validates( :currency_code, :presence => true ) end for now, helps me if map word form params[ :model ]. because attributes looked in params[ :model ] hash when process method called limited attributes explicitly listed in contract do/end block; which why strong parameters unneeded when using tbr.
note original test still fails because currency_code validation not met. intentional @ time test written.
Comments
Post a Comment