3

我有一个自引用的用户模型:

class User < ActiveRecord::Base
    has_and_belongs_to_many :following,
                            :class_name               => "User",
                            :foreign_key              => 'follower_id',
                            :association_foreign_key  => 'following_id',
                            :join_table               => 'followers'
    has_and_belongs_to_many :followers,
                            :class_name               => "User",
                            :foreign_key              => 'following_id',
                            :association_foreign_key  => 'follower_id',
                            :join_table               => 'followers'
    ...
end

我很难想出一个 FactoryGirl 工厂来测试这种行为。我尝试了一些变体

factory :user do
    ...
    factory :following do
        after(:create) do |user, evaluator|
            create_list(:user, evaluator.followers_count, follower: user)
        end
    end
end

但没有运气。你会怎么做?感谢您的任何意见。

4

0 回答 0