문제

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