質問

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