简体   繁体   中英

factory_girl factories with associations not assigning IDs correctly

I have 3 models that associate like so:

#user.rb
has_many :forums
has_many :posts

#forum.rb
belongs_to :user
has_many :posts

#post.rb
belongs_to :user
belongs_to :forum

I'm trying to create a single set of factories that all share the needed IDs needed to be associated with each other.

#User factory
FactoryGirl.define do
  factory :user do
    sequence :email do |n|
      "testuser#{n}@postfactory.com"
    end

    password "password#1"
  end
end

#Forum factory
FactoryGirl.define do
  factory :forum do
    user
    name "Forum Name"
    description "Forum Description with a minimum character count of 20"
  end
end

#Post factory
FactoryGirl.define do
  factory :post do
    user
    forum
    title 'Post 1'
    description 'This is a test description for Post 1'
  end
end

When I run my spec test with:

user =      FactoryGirl.create(:user)
forum =     FactoryGirl.create(:forum)
post =      FactoryGirl.create(:post)

It outputs the following in the console:

#<User id: 1, email: "testuser1@userfactory.com", created_at: "2016-10-27 20:10:36", updated_at: "2016-10-27 20:10:36">

#<Forum id: 1, name: "Forum Name", description: "Forum Description with a minimum character count o...", user_id: 2, created_at: "2016-10-27 20:10:36", updated_at: "2016-10-27 20:10:36">

#<Post id: 1, title: "Post 1", description: "This is a test description for Post 1", user_id: 3, forum_id: 2, created_at: "2016-10-27 20:10:36", updated_at: "2016-10-27 20:10:36">

As you can see, the user_id increments with each factory being created as well as forum_id . I would like these to all have the ID of 1 without having to do some manual work. What have I done incorrectly with my setup

Edit: I sort of see what I'm doing incorrectly. I only need to generate a post in my spec test and it will generate the factories needed (forum and user) to create the post . However, I do notice that I'm generating two users.

(byebug) User.count
2
(byebug) User.first
#<User id: 1, email: "testuser1@postfactory.com", created_at: "2016-10-27 20:30:33", updated_at: "2016-10-27 20:30:33">
(byebug) User.last
#<User id: 2, email: "testuser2@postfactory.com", created_at: "2016-10-27 20:30:33", updated_at: "2016-10-27 20:30:33">

Any idea why that is? I tried removing the sequence :email part and doing it standard. However, I get a validation error that the email has already been taken. For some reason, it's trying to run the user factory twice even though I call it only once in my spec test.

Every time you call FactoryGirl.create , there is a new created user, so after you run this code:

user  = FactoryGirl.create(:user)
forum = FactoryGirl.create(:forum)
post  = FactoryGirl.create(:post)

actually you created 3 users, as you can see post has user_id: 3 .

If you want to create forum and post with user you created, you can assign that user to forum and post when they are created:

user  = FactoryGirl.create(:user)
forum = FactoryGirl.create(:forum, user: user)
post  = FactoryGirl.create(:post, user: user, forum: forum)

With this code, there is only one created user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM