2016-03-22 2 views
0

ich ein Validate für meine Bestellung Modell hinzugefügt:RSPEC fehlgeschlagen validate: document_type ist nicht in der Liste enthalten

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 

Aber wenn ich laufen „rspec“ diese ausgefallen und zeigte mir dies:

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)>' 

Wie kann ich document_type zur Liste hinzufügen?

+0

Aktualisieren Sie die Frage mit 'spec/factories/order.rb' Inhalt. – Pavan

+0

@Pavan fertig: 'D – Giancarlos

+0

Versuchen Sie 'Dokumententyp [" Boleta "," Faktura "]. Beispiel' anstelle von 'Dokumententyp' MyString'' – Pavan

Antwort

1

mit den folgenden:

validates :document_type, inclusion: { in: %w(boleta factura) }, 
    allow_nil: true 

geben Sie an, dass document_typeboleta oder factura sein muss.

Allerdings setzt Ihre Factory document_type auf MyString, deshalb erhalten Sie den Validierungsfehler.

Um Ihr Problem zu ändern, um Ihre Fabrik zu lösen document_type als boleta oder factura zu setzen oder einfach nur das Feld entfernen, da Sie nil erlauben (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 
0

Umwickeln Sie ein Array-Beispiel in einem Block.

document_type { ["boleta", "factura"].sample } 
Verwandte Themen