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?

没有正确的解决方案

其他提示

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)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top