Marcin Doliwa
Marcin Doliwa

Reputation: 3659

Factory with arguments

I have simple factory:

FactoryGirl.define do
  factory :event_with_tags do
    name 'event-name'
    start_date '2016-03-16'
    start_time '11:34'
    tags = %w(foo bar)
    after(:create) do |event|
      tags.each do |tag|
        event.tags << create(:tag, name: tag)
      end
    end
  end
end

Is it possible to create factory in such a way, that I can use it like this or something similiar?:

FactoryGirl.create(:event_with_tags, tags: 'foo bar qux')

Upvotes: 2

Views: 2942

Answers (1)

Maxim
Maxim

Reputation: 9961

Yes, it is possible. You should use Transient Attributes i.e. declare tags as transient attribute.

Upvotes: 3

Related Questions