How can I force Forgery to return unqiue data within a Factory_girl definition
-
15-02-2021 - |
Question
My factory looks like this:
Factory.define :coupon do |c|
c.title { Forgery(:lorem_ipsum).sentences(3, :random => true) }
end
And my call from Rspec looks like this:
coupons = []
5.times {|i| coupons << Factory(:coupon, :starts_at => i.days.ago) }
With my Coupon model I have a validation with requires the title to be unique. The only way I am able to run this test is by doing this in my factory:
Factory.define :coupon do |c|
c.title {|i| Forgery(:lorem_ipsum).sentences(3, :random => true) + "#{i}" }
end
Surely there must be a better way?
No correct solution
OTHER TIPS
This is how I would do it (using the new Factory Girl syntax):
FactoryGirl.define do
factory :coupon do
sequence(:title) { |n| Forgery::LoremIpsum.words(n, :random => true) }
sequence(:starts_at) { |n| n.days.ago }
end
end
Then creating coupons in a spec:
coupons = FactoryGirl.create_list(:coupon, 5)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow