Calling methods from FactoryGirl model in dependent attribute

I have a model similar to the following:

class Foo
  attr_accessor :attribute_a  # Really an ActiveRecord attribute
  attr_accessor :attribute_b  # Also an ActiveRecord attribute

  def determine_attribute_b
    self.attribute_b = some_biz_logic(attribute_a)
  end
end

In FactoryGirl 1.3, I had a factory that looked like this:

Factory.define :foo do |foo|
  foo.attribute_a = "some random value"
  foo.attribute_b { |f| f.determine_attribute_b }
end

Everything went perfectly. attribute_bwas an attribute depending on the code block that would be passed to the real instance Foointo a variable f, complete with correctly set attribute_ato work.

I just upgraded to FactoryGirl 2.3.2 and this method no longer works. The variable ffrom the equivalent code to above is no longer an instance Foo, but instead of a FactoryGirl::Proxy::Create. This class, apparently, can read previously set attributes (for example, in the example of dependent attributes in documents ). However, it cannot call the actual methods from the built-in class.

FactoryGirl? , .

+5
1

after_create after_build :

FactoryGirl.define do
  factory :foo do
    attribute_a "some random value"
    after_build do |obj|
      obj.attribute_b = obj.determine_attribute_b
    end
  end
end

FactoryGirl.

+3

All Articles