首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RSPEC验证失败: Document_type未包括在列表中

RSPEC验证失败: Document_type未包括在列表中
EN

Stack Overflow用户
提问于 2016-03-22 17:39:01
回答 2查看 651关注 0票数 0

我为我的订单模型添加了一个验证:

代码语言:javascript
复制
validates :document_type, inclusion: { in: %w(boleta factura) },
  allow_nil: true

我的规范/命令/命令My:

代码语言:javascript
复制
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“时,它失败了,并向我展示了以下内容:

代码语言:javascript
复制
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添加到列表中?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-22 18:22:39

有以下几点:

代码语言:javascript
复制
validates :document_type, inclusion: { in: %w(boleta factura) },
  allow_nil: true

您正在指定document_type必须是boletafactura

但是,工厂将document_type设置为MyString,因此您将得到验证错误。

要解决您的问题,请将工厂设置为document_typeboletafactura或移除该字段,因为您允许零(allow_nil)。

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2016-03-22 18:30:32

将数组示例封装在块中。

代码语言:javascript
复制
document_type { ["boleta", "factura"].sample }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36161491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档