Home>
I want to solve

When running rspec, the test fails with the following error: I would like to ask about the error resolution method here.

Failures:
  1) User dependent: destoy destroys all posts when deleted
     Failure/Error: 2.times {FactoryBot.create (: post, user: user)}
     ActiveRecord :: RecordInvalid:
       Validation failed: Enter Create user


It says "Please enter Create user",
Since I am using the user created by FactoryBot, I thought that the user was created.
I suspect that the method of creating test data in FactoryBot with association is incorrect.
I'm guessing.
At the same time, I'm investigating "How to create test data with FactoryBot with association", but I can't find a way to resolve the error, so I'd like to ask.

Current status

I'm trying to unit test a user model that stores login user information.

require'rails_helper'
RSpec.describe User, type:: model do
  let (: user) {FactoryBot.create (: user)}
  # If i have a user ID, email, and password, it must be valid.
  it "is valid with a email and password" do
    expect (FactoryBot.build (: user)). to be_valid
  end
  # Delete dependency
  describe "dependent: destoy" do
    # If i delete it, all the stores associated with it will also be deleted.
    it "destroys all posts when deleted" do
      2.times {FactoryBot.create (: post, user: user)}
      expect {user.destroy} .to change (user.posts,: count) .by (-2)
    end
  end
end
Test data: Contents of FactoryBot factories/users.rb
FactoryBot.define do
  factory: user do
    sequence (: email) {| n | "test # {n} @ gmail.com"}
    password {"passwd"}
  end
end
factories/posts.rb
FactoryBot.define do
  factory: post do
    sequence (: title) {| n | "test post # {n}"}
    description {"Review test"}
    prefecture_code {"1"}
    rest_type {"1"}
    association: user
    association: community
  end
end
Contents of model

In the user model, there are associations with other models such as post,
When the record of the user model is deleted, the record of the post model associated with it is also deleted. The specific code is below.

models/user.rb (excerpt)
class User</pre>
post.rb (excerpt)
<pre><code data-language = "Ruby">class Post</pre>
<strong>What I tried</strong>
<p>① Thinking that create user should be created in the target test case of user_spec.rb,<br />
I wrote the following test code, but I still got the same error.</p>
<pre><code data-language = "Ruby"># Delete dependency
  describe "dependent: destoy" do
    # If i delete it, all the stores associated with it will also be deleted.
    it "destroys all posts when deleted" do
      post_user = FactoryBot.create (: user)
      2.times {FactoryBot.create (: post, user: post_user)}
      expect {post_user.destroy} .to change (post_user.posts,: count) .by (-2)
    end
  end

② Even if you quit 2.times and modify the post so that it is created only once and see if it is -1
I was still getting the exact same error.

(3) When I changed the description to create Post in it of the corresponding test case, the following error came to appear.

# Delete dependency
  describe "dependent: destoy" do
    # If i delete it, all the stores associated with it will also be deleted.
    it "destroys all posts when deleted" do
      post_user = FactoryBot.create (: user)
      Post.create (title: "1" * 6, description: "1" * 10, rest_type: "1", prefecture_code: "1", user_id: post_user.id)
      expect {post_user.destroy} .to change (post_user.posts,: count) .by (-1)
    end

1) User dependent: destoy destroys all posts when deleted
     Failure/Error: expect {post_user.destroy} .to change (post_user.posts,: count) .by (-1)
       expected `Post :: ActiveRecord_Associations_CollectionProxy # count` to have changed by -1, but was changed by 0


⇒ I think that "even if you delete the user, the linked posts did not disappear",
For user modelhas_many: posts, dependent:: destroy, Post modelbelongs_to: userIt is described as.

I'm sorry, but I'm just starting to learn Rspec, so
There may be some points that are overlooked in the basic parts.
If i have any questions or need additional information, please let us know.

  • Answer # 1

    There are two possibilities.
    1. 1. User's email is required to be unique. In bot, the sequence number is always made from 1 instead of random.
    If you create only one User, it will succeed, but if it is called many times, it will be created with the same number, and it may fail. The failure is used in FactoryBot.create (: post, user: post_user).
    Please check post_user at this timing

    It doesn't match the error message
    It seems that the required comunnity is not created in factories/posts.rb.