質問編集履歴

7

models/user.rbとmodels/post.rbの抜粋部分を増やして記載

2020/11/04 13:28

投稿

otdsh9432
otdsh9432

スコア55

test CHANGED
File without changes
test CHANGED
@@ -52,6 +52,16 @@
52
52
 
53
53
 
54
54
 
55
+ # ユーザID,メール,パスワードがあれば有効な状態であること
56
+
57
+ it "is valid with a email and password" do
58
+
59
+ expect(FactoryBot.build(:user)).to be_valid
60
+
61
+ end
62
+
63
+
64
+
55
65
  # 削除の依存関係
56
66
 
57
67
  describe "dependent: destoy" do
@@ -132,14 +142,48 @@
132
142
 
133
143
 
134
144
 
135
- ##### user.rb(抜粋)
145
+ ##### models/user.rb(抜粋)
136
146
 
137
147
  ```ruby
138
148
 
139
149
  class User < ApplicationRecord
140
150
 
151
+ devise :database_authenticatable, :registerable,
152
+
153
+ :recoverable, :rememberable, :validatable, :timeoutable, authentication_keys: [:email]
154
+
155
+ validates :email, presence: true, uniqueness: true
156
+
157
+ validates :username, length: { maximum: 30 }
158
+
159
+ has_one_attached :image
160
+
161
+
162
+
141
163
  has_many :posts, dependent: :destroy
142
164
 
165
+ has_many :likes, dependent: :destroy
166
+
167
+ has_many :comments , dependent: :destroy
168
+
169
+ has_many :belongings, dependent: :destroy
170
+
171
+ has_many :applies, dependent: :destroy
172
+
173
+
174
+
175
+ has_many :communities, through: :belongings # ユーザが所属しているコミュニティ
176
+
177
+
178
+
179
+ has_many :follower, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy # フォロー取得
180
+
181
+ has_many :followed, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy # フォロワー取得
182
+
183
+ has_many :following_user, through: :follower, source: :followed # 自分がフォローしている人
184
+
185
+ has_many :follower_user, through: :followed, source: :follower # 自分をフォローしている人
186
+
143
187
  end
144
188
 
145
189
  ```
@@ -152,8 +196,26 @@
152
196
 
153
197
  class Post < ApplicationRecord
154
198
 
199
+ validates :title, presence: true, length: { maximum:30 }
200
+
201
+ validates :description, presence: true, length: { maximum:200 }
202
+
203
+ validates :prefecture_code, presence: true
204
+
205
+ validates :rest_type, presence: true
206
+
207
+ has_one_attached :image
208
+
209
+
210
+
155
211
  belongs_to :user
156
212
 
213
+ belongs_to :community
214
+
215
+ has_many :likes, dependent: :destroy
216
+
217
+ has_many :comments, dependent: :destroy
218
+
157
219
  end
158
220
 
159
221
  ```

6

試したことを1つ追加

2020/11/04 13:28

投稿

otdsh9432
otdsh9432

スコア55

test CHANGED
File without changes
test CHANGED
@@ -164,7 +164,7 @@
164
164
 
165
165
 
166
166
 
167
- user_spec.rbの対象のテストケースの中でcreate userすべきかと考え、
167
+ user_spec.rbの対象のテストケースの中でcreate userすべきかと考え、
168
168
 
169
169
  以下のテストコードを記述しましたが、変わらず同じエラーが出ました。
170
170
 
@@ -194,12 +194,60 @@
194
194
 
195
195
 
196
196
 
197
- また、2.timesを辞めて、postは1回のみcreateするようにして-1されるかを見るように修正しても、
197
+ 2.timesを辞めて、postは1回のみcreateするようにして-1されるかを見るように修正しても、
198
198
 
199
199
  全く同じエラーが出るままでした。
200
200
 
201
201
 
202
202
 
203
+ ③該当テストケースのitの中でPostをcreateする記述に変えると、下記のエラーが出るようになりました。
204
+
205
+ <user_spec.rb>
206
+
207
+
208
+
209
+ ```ruby
210
+
211
+ # 削除の依存関係
212
+
213
+ describe "dependent: destoy" do
214
+
215
+ # 削除すると、紐づく店舗も全て削除されること
216
+
217
+ it "destroys all posts when deleted" do
218
+
219
+ post_user = FactoryBot.create(:user)
220
+
221
+ Post.create(title: "1"*6, description:"1"*10, rest_type:"1", prefecture_code:"1", user_id:post_user.id)
222
+
223
+ expect { post_user.destroy }.to change(post_user.posts, :count).by(-1)
224
+
225
+ end
226
+
227
+ ```
228
+
229
+
230
+
231
+ <エラー内容>
232
+
233
+ ```ruby
234
+
235
+ 1) User dependent: destoy destroys all posts when deleted
236
+
237
+ Failure/Error: expect { post_user.destroy }.to change(post_user.posts, :count).by(-1)
238
+
239
+ expected `Post::ActiveRecord_Associations_CollectionProxy#count` to have changed by -1, but was changed by 0
240
+
241
+ ```
242
+
243
+ ⇒「ユーザを消しても、紐づく投稿が消えなかった」ということかと思いますが、
244
+
245
+  userのmodelには `has_many :posts, dependent: :destroy` 、postのモデルには `belongs_to :user` と記述しています。
246
+
247
+
248
+
249
+
250
+
203
251
  申し訳ありませんが、まだRspecを学習し始めたばかりなこともあり、
204
252
 
205
253
  基本的な箇所で見逃している点等あるかもしれないです。

5

現状や試したことを記述

2020/11/02 02:14

投稿

otdsh9432
otdsh9432

スコア55

test CHANGED
File without changes
test CHANGED
@@ -194,6 +194,12 @@
194
194
 
195
195
 
196
196
 
197
+ また、2.timesを辞めて、postは1回のみcreateするようにして-1されるかを見るように修正しても、
198
+
199
+ 全く同じエラーが出るままでした。
200
+
201
+
202
+
197
203
  申し訳ありませんが、まだRspecを学習し始めたばかりなこともあり、
198
204
 
199
205
  基本的な箇所で見逃している点等あるかもしれないです。

4

現状や試したことを記述

2020/11/01 09:10

投稿

otdsh9432
otdsh9432

スコア55

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- rspec実行時、下記のエラーが出てテストが失敗になります。
5
+ rspec実行時、下記のエラーが出てテストが失敗になります。こちらのエラー解決方法についてお伺いしたいです。
6
6
 
7
7
  ```ruby
8
8
 

3

現状や試したことを記述

2020/11/01 09:08

投稿

otdsh9432
otdsh9432

スコア55

test CHANGED
@@ -1 +1 @@
1
- 【RSpec】model specにてエラー:「バリデーションに失敗しました: Create userを入力してください」の解消方法について
1
+ 【RSpec】dependent: destoy」のテストにてエラー:「バリデーションに失敗しました」の解消方法について
test CHANGED
File without changes

2

現状や試したことを記述

2020/11/01 08:42

投稿

otdsh9432
otdsh9432

スコア55

test CHANGED
@@ -1 +1 @@
1
- 【RSpec】model specでのdependent: destoyのテスト時のバリデーションエラーの解消方法
1
+ 【RSpec】model specにてエラー:「バリデーションに失敗しました: Create userを入力してください」の解消方法について
test CHANGED
File without changes

1

現状や試したことを記述

2020/11/01 07:54

投稿

otdsh9432
otdsh9432

スコア55

test CHANGED
File without changes
test CHANGED
@@ -22,7 +22,7 @@
22
22
 
23
23
  「Create userを入力してください」と出ていますが、
24
24
 
25
- FactoryBotで作成したuserを使っているため、userは作成されていると思っのですが、
25
+ FactoryBotで作成したuserを使っているため、userは作成されていると考えおりまし
26
26
 
27
27
  アソシエーションを伴うFactoryBotでのテストデータの作成方法が誤っているのではないかと
28
28
 
@@ -38,23 +38,101 @@
38
38
 
39
39
 
40
40
 
41
+ <対象テストコード(models/user_spec.rb)>
42
+
43
+ ```ruby
44
+
45
+ require 'rails_helper'
46
+
47
+
48
+
49
+ RSpec.describe User, type: :model do
50
+
51
+ let(:user) { FactoryBot.create(:user) }
52
+
53
+
54
+
41
- <対象テストコード>
55
+ # 削除の依存関係
56
+
42
-
57
+ describe "dependent: destoy" do
58
+
59
+ # 削除すると、紐づく店舗も全て削除されること
60
+
61
+ it "destroys all posts when deleted" do
62
+
63
+ 2.times { FactoryBot.create(:post, user: user) }
64
+
65
+ expect { user.destroy }.to change(user.posts, :count).by(-2)
66
+
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+
43
- ```
73
+ ```
74
+
44
-
75
+ ### テストデータ:FactoryBotの中身
76
+
77
+ ###### factories/users.rb
78
+
79
+ ```ruby
80
+
81
+ FactoryBot.define do
82
+
83
+ factory :user do
84
+
85
+ sequence(:email) { |n| "test#{n}@gmail.com" }
86
+
87
+ password { "passwd" }
88
+
89
+ end
90
+
91
+ end
92
+
45
- ``
93
+ ```
94
+
95
+
96
+
97
+ ###### factories/posts.rb
98
+
99
+ ```ruby
100
+
101
+ FactoryBot.define do
102
+
103
+ factory :post do
104
+
105
+ sequence(:title) { |n| "テスト投稿#{n}"}
106
+
107
+ description {"口コミのテストです"}
108
+
109
+ prefecture_code {"1"}
110
+
111
+ rest_type {"1"}
112
+
113
+ association :user
114
+
115
+ association :community
116
+
117
+ end
118
+
119
+ end
120
+
121
+ ```
122
+
123
+
124
+
125
+ ### modelの中身
46
126
 
47
127
 
48
128
 
49
129
  userモデルでは、post(投稿)などの他のモデルとアソシエーションがあり、
50
130
 
51
- userモデルのレコードが削除されると、それに紐づくpostモデルのレコードも削除されるようになっています。
131
+ userモデルのレコードが削除されると、それに紐づくpostモデルのレコードも削除されるようになっています。具体的なコードは以下です。
52
-
53
- 具体的なコードは以下です。
132
+
54
-
55
-
56
-
133
+
134
+
57
- ### user.rb(抜粋)
135
+ ##### user.rb(抜粋)
58
136
 
59
137
  ```ruby
60
138
 
@@ -68,14 +146,58 @@
68
146
 
69
147
 
70
148
 
71
- ### post.rb(抜粋)
149
+ ##### post.rb(抜粋)
72
-
150
+
73
- ```ruby
151
+ ```ruby
74
-
152
+
75
- class User < ApplicationRecord
153
+ class Post < ApplicationRecord
76
-
154
+
77
- has_many :posts, dependent: :destroy
155
+ belongs_to :user
78
-
156
+
79
- end
157
+ end
80
-
158
+
81
- ```
159
+ ```
160
+
161
+
162
+
163
+ ### 試したこと
164
+
165
+
166
+
167
+ user_spec.rbの対象のテストケースの中でcreate userすべきかと考え、
168
+
169
+ 以下のテストコードを記述しましたが、変わらず同じエラーが出ました。
170
+
171
+
172
+
173
+ ```ruby
174
+
175
+ # 削除の依存関係
176
+
177
+ describe "dependent: destoy" do
178
+
179
+ # 削除すると、紐づく店舗も全て削除されること
180
+
181
+ it "destroys all posts when deleted" do
182
+
183
+ post_user = FactoryBot.create(:user)
184
+
185
+ 2.times { FactoryBot.create(:post, user: post_user) }
186
+
187
+ expect { post_user.destroy }.to change(post_user.posts, :count).by(-2)
188
+
189
+ end
190
+
191
+ end
192
+
193
+ ```
194
+
195
+
196
+
197
+ 申し訳ありませんが、まだRspecを学習し始めたばかりなこともあり、
198
+
199
+ 基本的な箇所で見逃している点等あるかもしれないです。
200
+
201
+ 不明点や追加必要な情報ございましたら、ご教示いただけますと幸いです。
202
+
203
+ よろしくお願いいたします。