DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on

Making an array of test data by Factorybot

🤔 ok but not great

Because you can't understand it without reading through the procedure.

let(:author1) { create(:author) }
let(:author2) { create(:author) }
let(:book1) { create(:book, author: author1) }
let(:book2) { create(:book, author: author2) }
let(:books) { [book1, book2] }

👍 Great

The intention is clear. The test needs the books array, and each book has different author.

let(:authors){ create_list(:author, 2) }
let(:books){
    [
      create(:book, author: authors[0]),
      create(:book, author: authors[1])
    ]
  }

do-end is ok instead of the {}

let(:companies) do  [
    create(:company, name: 'Hoge Company'),
    create(:company, name: 'Fuga Company'),
    create(:company, name: 'Piyo Company')
] end

🔗 Parent Note

Top comments (0)