FactoryGirl has_many与验证关联 [英] FactoryGirl has_many association with validation

查看:62
本文介绍了FactoryGirl has_many与验证关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标准的has_many关系(预订有许多订单),并确认没有至少一个订单不会保存预订.我正在尝试使用FactoryGirl工厂复制此文件,但是验证阻止我这样做.

I have a standard has_many relationship (Booking has many Orders) with validation that a Booking does not get saved without at least one Order. I'm trying to replicate this with my FactoryGirl factories but the validation is preventing me from doing so.

class Booking < ActiveRecord::Base
  has_many :orders
  validates :orders, presence: true
end

class Order < ActiveRecord::Base
  belongs_to :booking
end

这是我对每种型号的FactoyGirl工厂规格,其后是FactoryGirl的GitHub Wiki页面.

Here are my FactoyGirl factory specifications for each model as followed from FactoryGirl's GitHub wiki page.

FactoryGirl.define do                                                    

  factory :booking do                                                                                                                   
    factory :booking_with_orders do

      ignore do                                                                                                                         
        orders_count 1                                                                                                                  
      end                                                                                                                               

      before(:create) do |booking, evaluator|                                                                                           
        FactoryGirl.create_list(:order, evaluator.orders_count, booking: booking)                                                       
      end                                                                                                                               
    end                                                                                                                                 
  end 

  factory :order do
    booking
  end

end 

当我尝试从规格中运行FactoryGirl.create(:booking_with_orders)时,我得到:

When I try to run FactoryGirl.create(:booking_with_orders) from my spec, I get:

Failure/Error: @booking = FactoryGirl.create(:booking_with_orders)
ActiveRecord::RecordInvalid:
  Validation failed: Orders can't be blank

似乎已经在before(:create) [...]之前运行了验证检查,这从理论上说将为预订创建订单.

It seems like the check for the validation is running even before before(:create) [...] which would theoretically create the Orders for the Booking.

这篇文章建议不要将has_many关系添加到您的工厂,但无论如何我都想解决这个问题.

This post recommends not adding has_many relationships to your factories but I would like to solve this anyways if there is a good way to do it.

谢谢.

推荐答案

Wat?不可能的?完全没有.

Wat? Impossible? Not at all.

只需将您的代码更改为以下内容:

Just change your code to something like this:

after :build do |booking, evaluator|
  booking.orders << FactoryGirl.build_list(:order, evaluator.orders_count, booking: nil)
end

这篇关于FactoryGirl has_many与验证关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆