如何在FactoryGirlRails中定义序列? [英] How do I define sequences in FactoryGirlRails?

查看:49
本文介绍了如何在FactoryGirlRails中定义序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前在Factory girl中,我们可以这样定义序列:

Previously in Factory girl, we could define sequences like so:

# /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

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

Now, when I try that approach - I get a deprecation warning telling me:

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

将#next替换为#run时,出现无方法错误. 我在任何文档中都找不到新的语法...有人能指出我正确的方向吗?

When I replace #next with #run, I get a no-method error. I can't find the new syntax in any of the docs... Can anyone point me in the right direction?

谢谢

推荐答案

我认为您应该改用Factory.create(...),例如

I think you should use Factory.create(...) instead, e.g.

token { Factory.create(:random_token) }

这篇关于如何在FactoryGirlRails中定义序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆