我为我的订单模型添加了一个验证:
validates :document_type, inclusion: { in: %w(boleta factura) },
allow_nil: true我的规范/命令/命令My:
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添加到列表中?
发布于 2016-03-22 18:22:39
有以下几点:
validates :document_type, inclusion: { in: %w(boleta factura) },
allow_nil: true您正在指定document_type必须是boleta或factura。
但是,工厂将document_type设置为MyString,因此您将得到验证错误。
要解决您的问题,请将工厂设置为document_type为boleta或factura或移除该字段,因为您允许零(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发布于 2016-03-22 18:30:32
将数组示例封装在块中。
document_type { ["boleta", "factura"].sample }https://stackoverflow.com/questions/36161491
复制相似问题