質問編集履歴

3

コードを全文記載しました。

2020/10/29 02:17

投稿

tarotarotarotar
tarotarotarotar

スコア41

test CHANGED
File without changes
test CHANGED
@@ -65,8 +65,6 @@
65
65
  @user.nickname = ''
66
66
 
67
67
  @user.valid?
68
-
69
- binding.pry
70
68
 
71
69
  end
72
70
 

2

変更

2020/10/29 02:17

投稿

tarotarotarotar
tarotarotarotar

スコア41

test CHANGED
File without changes
test CHANGED
@@ -78,26 +78,6 @@
78
78
 
79
79
  end
80
80
 
81
- it "emailが空では登録できない" do
82
-
83
- end
84
-
85
- it "重複したemailが存在する場合登録できない" do
86
-
87
- end
88
-
89
- it "passwordが空では登録できない" do
90
-
91
- end
92
-
93
- it "passwordが5文字以下であれば登録できない" do
94
-
95
- end
96
-
97
- it "passwordが存在してもpassword_confirmationが空では登録できない" do
98
-
99
- end
100
-
101
81
  end
102
82
 
103
83
  end

1

マークダウンに変更

2020/10/29 02:17

投稿

tarotarotarotar
tarotarotarotar

スコア41

test CHANGED
File without changes
test CHANGED
@@ -12,13 +12,143 @@
12
12
 
13
13
  25行目でnicknameを空にしているので通常であればエラーとなるのですが、テストコードを正常に通過してしまいます。
14
14
 
15
+ 【spec>models>user_spec.rb】
16
+
17
+ ```require 'rails_helper'
18
+
19
+ describe User do
20
+
21
+ before do
22
+
23
+ @user = FactoryBot.build(:user)
24
+
25
+ end
26
+
27
+
28
+
29
+ describe 'ユーザー新規登録' do
30
+
31
+ context '新規登録がうまくいくとき' do
32
+
33
+ it "nicknameとemail、passwordとpassword_confirmationが存在すれば登録できる" do
34
+
35
+ expect(@user).to be_valid
36
+
37
+ end
38
+
39
+ it "nicknameが8文字以下で登録できる" do
40
+
41
+ @user.nickname = "aaaaaaaa"
42
+
43
+ expect(@user).to be_valid
44
+
45
+ end
46
+
47
+ it "passwordが6文字以上であれば登録できる" do
48
+
49
+ @user.password = "000000"
50
+
15
- ![イメージ説明](74e4682be43a380ebddc850878f279af.png)
51
+ @user.password_confirmation = "000000"
52
+
53
+ expect(@user).to be_valid
54
+
55
+ end
56
+
57
+ end
58
+
59
+
60
+
61
+ context '新規登録がうまくいかないとき' do
62
+
63
+ it "nicknameが空だと登録できない" do
64
+
65
+ @user.nickname = ''
66
+
67
+ @user.valid?
68
+
69
+ binding.pry
70
+
71
+ end
72
+
73
+ it "nicknameが9文字以上であれば登録できない" do
74
+
75
+ @user.nickname = 'aaaaabccccccccccccccccbbb'
76
+
77
+ @user.valid?
78
+
79
+ end
80
+
81
+ it "emailが空では登録できない" do
82
+
83
+ end
84
+
85
+ it "重複したemailが存在する場合登録できない" do
86
+
87
+ end
88
+
89
+ it "passwordが空では登録できない" do
90
+
91
+ end
92
+
93
+ it "passwordが5文字以下であれば登録できない" do
94
+
95
+ end
96
+
97
+ it "passwordが存在してもpassword_confirmationが空では登録できない" do
98
+
99
+ end
100
+
101
+ end
102
+
103
+ end
104
+
105
+ end
106
+
107
+ コード
108
+
109
+ ```
16
110
 
17
111
 
18
112
 
19
113
  以下のように10行目でnicknameカラムにpresence: trueのバリデーションをかけているのですが、テストが正常に通過してしまいます。
20
114
 
115
+ 【app>models>users.rb】
116
+
117
+ ```class User < ApplicationRecord
118
+
119
+ # Include default devise modules. Others available are:
120
+
121
+ # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
122
+
123
+ devise :database_authenticatable, :registerable,
124
+
125
+ :recoverable, :rememberable, :validatable
126
+
127
+ has_many :nutritions
128
+
129
+ has_many :favorites, dependent: :destroy
130
+
131
+ has_many :fav_nutritions, through: :favorites, source: :nutrition
132
+
133
+
134
+
135
+ validates :nickname, presence: true, length: { maximum: 8 }
136
+
137
+
138
+
21
- ![イメージ説明](793f6fbe9b202a2f5322d8609ef2ef3e.png)
139
+ def already_favorited?(nutrition, current_user)
140
+
141
+ Favorite.exists?(user_id: current_user.id, nutrition_id: nutrition.id)
142
+
143
+ end
144
+
145
+ end
146
+
147
+
148
+
149
+ コード
150
+
151
+ ```
22
152
 
23
153
 
24
154