문제

I want to reference the class represented by the factory during factory creation. Is that possible?

Here's what I'm doing now:

factory :foo do # Foo class
  name "Foo"
  ...
  initialize_with { Foo.some_complicated_method(attributes) }

  factory :bar_foo do
    name "Bar"
    initialize_with { BarFoo.some_complicated_method(attributes) }
  end
end

What I'd like to do is reference the class name is such a way that I can do this (replacing __factory_class__ with the appropriate method call):

factory :foo do
  name "Foo"
  ...
  initialize_with { __factory_class__.some_complicated_method(attributes) }

  factory :bar_foo do # Runs initialize_with using BarFoo instead of Foo
    name "Bar"
  end
end

Is what I'm wanting possible? Note that BarFoo is a subclass of Foo.

도움이 되었습니까?

해결책

It's not possible. It's described in FactoryGirl's Getting Started docs in Custom Construction section:

When using initialize_with, you don't have to declare the class itself when calling new; however, any other class methods you want to call will have to be called on the class explicitly.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top