Ruby on rails RSPEC验证失败:列表中不包括文档类型
我为我的订单模型添加了验证:Ruby on rails RSPEC验证失败:列表中不包括文档类型,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我为我的订单模型添加了验证: validates :document_type, inclusion: { in: %w(boleta factura) }, allow_nil: true My spec/fatories/orders.rb: FactoryGirl.define do factory :order do status 'MyString' order_date '2016-02-16 13:44:01' delivery_date '2016-02-16
validates :document_type, inclusion: { in: %w(boleta factura) },
allow_nil: true
My spec/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)>'
1)OrderItemsController后期创建重定向
失败/错误:@order=FactoryGirl.create(:order)
ActiveRecord::RecordInvalid:
验证失败:列表中不包括文档类型
#./spec/controllers/order\u items\u controller\u spec.rb:4:in“block(3层)in”
2) OrderItemsController删除销毁重定向
失败/错误:@order=FactoryGirl.create(:order)
ActiveRecord::RecordInvalid:
验证失败:列表中不包括文档类型
#./spec/controllers/order_items_controllers_spec.rb:37:in“block(3级)in”
3) OrderItemsController放置更新重定向
失败/错误:@order=FactoryGirl.create(:order)
ActiveRecord::RecordInvalid:
验证失败:列表中不包括文档类型
#./spec/controllers/order_items_controller_spec.rb:26:in `分块(3级)in'
4) OrderItemsController修补程序更新重定向
失败/错误:@order=FactoryGirl.create(:order)
ActiveRecord::RecordInvalid:
验证失败:列表中不包括文档类型
#./spec/controllers/order\u items\u controller\u spec.rb:15:in“block(3层)in”
如何将文档类型添加到列表中?包括以下内容:
validates :document_type, inclusion: { in: %w(boleta factura) },
allow_nil: true
您指定的document\u type
必须是boleta
或factura
但是,您的工厂正在将document\u type
设置为MyString
,因此您会收到验证错误
要解决您的问题,请将工厂设置为将document\u type
设置为boleta
或factura
,或者删除该字段,因为您允许nil(allow\u nil
)
以下是:
validates :document_type, inclusion: { in: %w(boleta factura) },
allow_nil: true
您指定的document\u type
必须是boleta
或factura
但是,您的工厂正在将document\u type
设置为MyString
,因此您会收到验证错误
要解决您的问题,请将工厂设置为将document\u type
设置为boleta
或factura
,或者删除该字段,因为您允许nil(allow\u nil
)
将阵列样本包装在块中
document_type { ["boleta", "factura"].sample }
将阵列样本包装在块中
document_type { ["boleta", "factura"].sample }
使用spec/factories/order.rb
content更新问题。尝试document\u type[“boleta”,“factura”]。示例而不是document\u type'MyString'
@Pavan its not work:(,它显示了相同的故障使用spec/factories/order.rb
content更新问题。尝试document\u type[“boleta”,“factura”].sample
而不是document\u键入'MyString'
@Pavan its not work:(,它向我显示相同的失败