Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RSPEC验证失败:列表中不包括文档类型_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

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:(,它向我显示相同的失败