Ruby on rails 问:;A.开拓者测试-合同方法的影响
以下问答基于《开拓者》一书第50-60页中给出的示例,并根据我的具体要求进行了调整。你可以简单地把Ruby on rails 问:;A.开拓者测试-合同方法的影响,ruby-on-rails,ruby,trailblazer,Ruby On Rails,Ruby,Trailblazer,以下问答基于《开拓者》一书第50-60页中给出的示例,并根据我的具体要求进行了调整。你可以简单地把ARInvoice和ar\u invoice想象成Thing和Thing,如果你在书中这样做的话,就可以得到一般的大意 我的operation.rb文件是: class ARInvoice < GLTransaction class Create < Trailblazer::Operation include( Model ) model( ARInvoice
ARInvoice
和ar\u invoice
想象成Thing
和Thing
,如果你在书中这样做的话,就可以得到一般的大意
我的operation.rb
文件是:
class ARInvoice < GLTransaction class Create <
Trailblazer::Operation
include( Model )
model( ARInvoice, :create )
contract() do
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 to use instance variable here
validate( params[ :ar_invoice ], @model ) do |f|
f.save
end
end
end
end
我得到了这个错误:
ar_invoice crud Create#test_0001_INSERTs a valid invoice: \
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: \
gl_transactions.effective_from may not be NULL: \
INSERT INTO "gl_transactions" . . .
但我也看到了这一点:
# Running:
2016-02-08 11:24:35 -0500
E
因此,设置了测试时间值。为什么它没有从属性进入有效_
我在下面给出了答案。问题是我没有理解《开拓者》(TBR)中使用的合同do/end
块的意义。我最初读这本书的方式是合同与形式有关。我在RoR方面的经验使我将单词形式映射到单词视图。我的错误。但也可能绊倒其他人
正确的合同块包含缺少的属性:effective\u from
contract() do
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
现在,如果我将单词form
映射到params[:model]
,它会帮助我。因为当调用过程
方法时,在参数[:model]
散列中查找的属性仅限于合同
执行/结束
块中明确列出的那些属性;这就是为什么在使用TBR时不需要强参数的原因
请注意,原始测试仍然失败,因为未满足currency\u code
验证。但在编写此测试时,这是有意的
contract() do
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