FactoryGirl瞬态变量的未定义方法

时间:2014-08-26 16:01:27

标签: ruby-on-rails ruby-on-rails-4 rspec factory-bot

我在FactoryGirl中使用version 4.4。我试图创建after(:create)回调,但我没有评估变量。

FactoryGirl.define do
  factory :organization do

    name 'example'

    factory :organization_with_links do

      transient do
        links_count 5
      end

      after(:create) do |organization, evaluator|
        create_list(:link, evaluator.links_count, organization: organization)
      end
    end
  end
end

不幸的是我得到了

NoMethodError: undefined method `links_count' for  FactoryGirl::SyntaxRunner:0x007fcefb1ff780>

根据官方工厂女孩指南,我正确地做了一切:https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#associations

如何让transient variable工作?

1 个答案:

答案 0 :(得分:34)

所以解决方案是改变

transient do
   links_count 5
end

ignore do
  links_count 5
end

原因:主人的文档已准备好FactoryGirl v.5.0。瞬态将在5.0发布时开始。

更多阅读:http://github.com/thoughtbot/factory_girl/issues/658