The Fabrication gem allows us to pass custom arguments to the constructor of the class we are fabricating:

on_init { init_with('something', true) }

But how can I pass custom values for init_with in object generation time?

For the object fields, I can do like the following, but is there a way to pass the values to init_with?

Fabricate(:foobar, attr1: 'something', attr2: true)
有帮助吗?

解决方案 2

As far as I can see, this is not possible.

  def build_instance_with_init_callback(callback)
    self._instance = _klass.new(*callback.call)
    set_attributes
  end

You are stuck with what you used in the fabricator definition.

其他提示

You can use the block syntax at Fabricate time just like you would when you define the Fabricator.

Fabricate(:foobar, attr1: 'something', attr2: true) do
  on_init { init_with('another', 'thing') }
end
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top