ruby on rails - Validating That A has_many Association Has At Least One Model When Using FactoryGirl -
ruby on rails - Validating That A has_many Association Has At Least One Model When Using FactoryGirl -
putting aside arguments on whether or not should test existence of model's associations, have model called order
, validating has @ to the lowest degree 1 item
in has_many
association using:
class order < activerecord::base has_many :items validates :items, presence: true end
i have set factorygirl lint factories (checking validity). order mill not valid unless create item has_many
collection.
my orders
mill looks this:
factorygirl.define mill :order ignore items_count 1 end after(:build) |order, evaluator| create_list(:item, evaluator.items_count, order: order) end end end
according mill girl's getting started:
factorygirl.lint builds each mill , subsequently calls #valid? on
however when run specs, mill girl throws factorygirl::invalidfactoryerror
because order mill invalid.
workaround
after(:build) |order, evaluator| evaluator.items_count.times order.items << factorygirl.create(:item) end #create_list(:item, evaluator.items_count, order: order) end
according definition, phone call .valid?
after building. seems phone call before running after(:build)
block.
try writing mill this:
factorygirl.define mill :order ignore items_count 1 end items { build_list(:item, items_count) } end end
this should build item before .valid?
called.
let me know if works :)
ruby-on-rails ruby rspec factory-girl factory
Comments
Post a Comment