3

I have this fabricator for a Rails model:

Fabricator(:foo) do
  transient n_iterations: 5

  bar(count: :n_iterations) { Fabricate(:bar) }
end

However, this doesn't work as hoped. I've tried count: n_iterations, count: attrs[:n_iterations], and count: attrs[n_iterations], and nothing seems to work. This seems like it should be a relatively simple thing to do though.

Any thoughts would be much appreciated.

JacobEvelyn
  • 3,901
  • 1
  • 40
  • 51

1 Answers1

0

You can use the following:

Fabricator(:foo) do
  transient n_iterations: 5
  bar { |attrs| (1..attrs[:n_iterations]).map { Fabricate(:bar) } }
end

Note, however, that Fabricate(:foo, n_iterations: 0) will return a Foo with bar => [] an empty array.

Brett Pontarelli
  • 1,718
  • 15
  • 29