【问题标题】:Rails Factory Girl create a list with relation one to manyRails Factory Girl 创建一个具有一对多关系的列表
【发布时间】:2017-09-24 12:10:25
【问题描述】:

我有这两个工厂:

factory :video do
  name "Elixir 101 class"
  description "A class of elixir"
  descriptor "111111"
  user    
end

sequence :email { |n| "test#{n}@email.com" }

factory :user do
  email FactoryGirl::generate(:email)
  password 'password'
end

这种关系是“一个用户有很多视频”。所以用户has_many视频和视频belongs_to用户

我正在尝试使用 let!(:videos) { create_list(:video, 3) } 创建包含视频的列表,但是当 Factory 要创建第二个视频时,验证 Email has already been taken 会弹出。我需要做什么才能使用同一用户创建所有 3 个视频,或者使用 3 个不同的电子邮件创建 3 个不同的用户。

提前致谢!

【问题讨论】:

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


    【解决方案1】:

    使用sequence

    文档为您提供了这个示例:

      sequence :email do |n|
        "person#{n}@example.com"
      end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多