Ruby on rails FactoryBot为联接表生成内容

Ruby on rails FactoryBot为联接表生成内容,ruby-on-rails,activerecord,factory-bot,factory,Ruby On Rails,Activerecord,Factory Bot,Factory,我正在尝试使用factory bot为RSpec生成测试数据。我的表格如下: User->可以是pro\u team\u player或noob\u team\u player,并且有一个模型对话: class Conversation < ActiveRecord::Base has_many :messages belongs_to :pro_team_player belongs_to :noob_team_player end 现在我可以生成上述数据,如下所示: us

我正在尝试使用factory bot为RSpec生成测试数据。我的表格如下:

User
->可以是
pro\u team\u player
noob\u team\u player
,并且有一个模型
对话

class Conversation < ActiveRecord::Base
  has_many :messages
  belongs_to :pro_team_player
  belongs_to :noob_team_player
end
现在我可以生成上述数据,如下所示:

user1 = FactoryBot.create(:user)
pro_team_player = build(:pro_team_player)
user1 = pro_team_player.user1

user2 = FactoryBot.create(:user)
noob_team_player = build(:pro_team_player)
user2 = noob_team_player.user2

我仍在学习FactoryBot,我不知道如何创建
对话
工厂或为此生成数据。如有任何帮助,我们将不胜感激

您已步入正轨。继续遵循您正在使用的模式:

FactoryBot.define do
  factory :conversation do
    pro_team_player
    noob_team_player
    # Other required conversation attributes, if any
  end
end

FactoryBot.define do
  factory :message do
    conversation
    # Other required message attributes, if any
  end
end
FactoryBot.define do
  factory :conversation do
    pro_team_player
    noob_team_player
    # Other required conversation attributes, if any
  end
end

FactoryBot.define do
  factory :message do
    conversation
    # Other required message attributes, if any
  end
end