Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequence within trait of FactoryGirl factory does not use main sequence counter

I have the following factory:

FactoryGirl.define do
  factory :foo do
    sequence(:name) { |n| "Foo #{n}" }

    trait :y do
      sequence(:name) { |n| "Fooy #{n}" }
    end
  end
end

If I run

create :foo
create :foo
create :foo, :y

I get Foo 1, Foo 2, Fooy 1. But I want Foo1, Foo2, Fooy 3. How can I achieve this?

like image 899
dav_i Avatar asked Jun 16 '15 16:06

dav_i


Video Answer


1 Answers

After a couple of hints from smile2day's answer and this answer, I came to the following solution:

FactoryGirl.define do
  sequence :base_name do |n|
    " #{n}"
  end

  factory :foo do
    name { "Foo " + generate(:base_name) }

    trait :y do
      name { "Fooy " + generate(:base_name) }
    end
  end
end
like image 111
dav_i Avatar answered Sep 28 '22 15:09

dav_i