我为订单模型添加了验证:
validates :document_type, inclusion: { in: %w(boleta factura) },
allow_nil: true
我的规格/ fatories / orders.rb:
FactoryGirl.define do
factory :order do
status 'MyString'
order_date '2016-02-16 13:44:01'
delivery_date '2016-02-16 13:44:01'
subtotal '9.99'
igv '9.99'
total '9.99'
document_type 'MyString'
store { FactoryGirl.build(:store) }
order_items { [FactoryGirl.build(:order_item)] }
user { FactoryGirl.build(:user) }
end
end
但是当我跑步" rspec"这失败了,并告诉我这个:
1) OrderItemsController POST create redirects
Failure/Error: @order = FactoryGirl.create(:order)
ActiveRecord::RecordInvalid:
Validate failed : Document type is not included in the list
# ./spec/controllers/order_items_controller_spec.rb:4:in `block (3 levels) in <top (required)>'
2) OrderItemsController DELETE destroy redirects
Failure/Error: @order = FactoryGirl.create(:order)
ActiveRecord::RecordInvalid:
Validate failed : Document type is not included in the list
# ./spec/controllers/order_items_controller_spec.rb:37:in `block (3 levels) in <top (required)>'
3) OrderItemsController PUT update redirects
Failure/Error: @order = FactoryGirl.create(:order)
ActiveRecord::RecordInvalid:
Validate failed : Document type is not included in the list
# ./spec/controllers/order_items_controller_spec.rb:26:in `block (3 levels) in <top (required)>'
4) OrderItemsController PATCH update redirects
Failure/Error: @order = FactoryGirl.create(:order)
ActiveRecord::RecordInvalid:
Validate failed : Document type is not included in the list
# ./spec/controllers/order_items_controller_spec.rb:15:in `block (3 levels) in <top (required)>'
如何将document_type
添加到列表中?
答案 0 :(得分:2)
以下内容:
validates :document_type, inclusion: { in: %w(boleta factura) },
allow_nil: true
您指定document_type
必须是boleta
或factura
。
但是,您的工厂正在将document_type
设置为MyString
,因此您将收到验证错误。
要解决您的问题,请更改工厂,将document_type
设置为boleta
或factura
或删除该字段,因为您允许使用nil(allow_nil
)。
FactoryGirl.define do
factory :order do
status 'MyString'
order_date '2016-02-16 13:44:01'
delivery_date '2016-02-16 13:44:01'
subtotal '9.99'
igv '9.99'
total '9.99'
document_type 'boleta' # or factura or remove this line
store { FactoryGirl.build(:store) }
order_items { [FactoryGirl.build(:order_item)] }
user { FactoryGirl.build(:user) }
end
答案 1 :(得分:0)
将数组样本包装在一个块中。
document_type { ["boleta", "factura"].sample }