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.
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.
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: user
It 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
Related articles
- ruby on rails - rspec test changed by -1, but was changed by 0 error
- ruby - i get an error when testing rspec instance variables
- ruby on rails - rspec validation error
- ruby - i want to resolve an error when installing gem byebug
- ruby on rails - i get an error in a program that changes over time
- ruby on rails - nomethoderror in itemscontroller # new error
- ruby - i got a routing error when i sent a comment
- ruby on rails - i get an error without being created
- ruby on rails 6 - when i press new user registration, i get an error of no route matches [get] "/ users"
- ruby on rails - i want to write a rails tutorial test in rspec
- ruby - i entered secret_key_base, but http error 500 cannot be resolved
- ruby on rails - i get an error with rails db: create
- ruby on rails - error when implementing rails6 follow function
- ruby - i get an error with a single test code for a model
- ruby error code
- ruby on rails uninitialized constant error
- ruby - i got "mysql2 :: error :: connectionerror: plugin caching_sha2_password could not be loaded "
- ruby on rails - phenomenon that the test that should be an "rspec" error passes
- ruby on rails - i don't understand the content of the error
- ruby - sass :: syntaxerror on rails s
- javascript - i want the scroll bar to always appear at the bottom of the chat screen
- ruby - i want to use two rails renders
- ruby - the top screen is displayed at http: // localhost: 3000/users/sign_in
- ruby - [rails] the class attribute specified by the div tag enclosed in simple_format is not applied
- ruby on rails removal function
- ruby on rails sorting
- html - i can't set the regular expression well
- ruby - rails i want to make the created_at time the same when creating multiple data at once
- ruby - dynamic form implementation in nested_form
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.