我想为包含两个属性的关系模型创建一个工厂,followed_id
但follower_id
我不知道该怎么做,这是我的工厂文件:
FactoryGirl.define do
factory :user do
sequence(:name) { |n| "Person #{n}" }
sequence(:email) { |n| "person_#{n}@example.com"}
password "foobar"
password_confirmation "foobar"
end
factory :relationship do
# i need something like this
# followed_id a_user.id
# follower_id another_user.id
end
end
更新
我想用这个关系工厂做的是测试如果我摧毁一个用户,他所有的关系也会被摧毁,这是我的测试:
describe "relationships associations" do
let!(:relationship) { FactoryGirl.create(:relationship) }
it "should destroy associated relationships" do
relationships = @user.relationships.to_a
@user.destroy
expect(relationships).not_to be_empty
relationships.each do |relationship|
expect(Relationships.where(id: relationship.id)).to be_empty
end
end
结尾