Michael Parton
Michael Parton

Reputation: 82

Factory Girl create_list with trait and dynamic variable

I'm trying to create some extra records from a Factorygirl callback. It has to use one of the traits in that factory and it has to pass it a dynamic variable. I thought I could do something like this:

create_list(:some_factory, 5, :some_trait, dynamic_variable: 1)

But I get an undefined method `dynamic_variable=' on the ActiveRecord object

Upvotes: 1

Views: 4949

Answers (1)

fossil
fossil

Reputation: 770

You need to define the transient in factory. Here is an example, I copied from wiki

factory :user do
  transient do
    rockstar true
    upcased  false
  end

  name  { "John Doe#{" - Rockstar" if rockstar}" }
  email { "#{name.downcase}@example.com" }

  after(:create) do |user, evaluator|
    user.name.upcase! if evaluator.upcased
  end
end

create_list(:user, 10, upcased: true).name

Upvotes: 4

Related Questions