FactoryBot: create the same object multiple times

47,348

You can create a list like this (hence create x objects at once):

FactoryBot.create_list(:model_1, 3)

Documentation lives here.

Note: FactoryBot was originally named FactoryGirl

Share:
47,348
usha
Author by

usha

Passionate ruby on rails developer here to learn by answering and reading questions.

Updated on August 26, 2020

Comments

  • usha
    usha over 3 years

    In one of my RSpec test, I am creating multiple objects from the same factory definition

    Eg

    FactoryBot.create(:model_1)
    FactoryBot.create(:model_1)
    FactoryBot.create(:model_1)
    

    Is there a method that factory_bot provides to do this in one line

    I know that I can do

    3.times {FactoryBot.create(:model_1)}
    

    But I am looking for something that factory_bot provides for creating multiple objects of the same model.

    Note: FactoryBot was originally named FactoryGirl