如何在Factory_Girl_Rails中定义序列?

时间:2011-03-31 09:33:28

标签: ruby-on-rails-3 rspec2 factory-bot

以前在Factory girl中,我们可以定义如下序列:

# /spec/factories.rb

FactoryGirl.define do

  # this is the sequence in question:
  sequence(:random_token) { Digest::MD5.hexdigest(rand.to_s) }

  factory :story do
    sequence(:title) { |n| "My Cool Story##{n}"  }
    # Call the sequence here:
    token { Factory.next(:random_token) }
    description { "#{title} description"}
  end

end

现在,当我尝试这种方法时 - 我收到一个弃用警告告诉我:

WARNING: FactoryGirl::Sequence#next is deprecated.
Use #run instead.

当我用#run替换#next时,我得到一个无方法错误。 我无法在任何文档中找到新语法......有人能指出我正确的方向吗?

由于

1 个答案:

答案 0 :(得分:9)

我认为你应该使用Factory.create(...)代替,例如

token { Factory.create(:random_token) }