質問編集履歴

2

内容の追加

2017/10/17 20:40

投稿

sakas1231
sakas1231

スコア42

test CHANGED
File without changes
test CHANGED
@@ -147,3 +147,187 @@
147
147
  end
148
148
 
149
149
  ```
150
+
151
+
152
+
153
+ --------------------------------------------------
154
+
155
+ [修正内容]
156
+
157
+ モデル側のバリデーションとのことで、user.rbとuser_test.rbを載せさせていただきます。
158
+
159
+ ちなみにreils testは通ってます(全然テスト書いていないのであたりまえかもしれないですが)
160
+
161
+ user.rb
162
+
163
+
164
+
165
+ ```
166
+
167
+ class User < ApplicationRecord
168
+
169
+ has_many :books, dependent: :destroy
170
+
171
+ before_save { self.email = email.downcase }
172
+
173
+ validates :name, presence: true, length: { maximum: 50 }
174
+
175
+ VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i
176
+
177
+ validates :email, presence: true, length: { maximum: 255 },
178
+
179
+ format: { with: VALID_EMAIL_REGEX },
180
+
181
+ uniqueness: { case_sensitive: false }
182
+
183
+ validates :budget, numericality: true
184
+
185
+ validates :password, presence: true, length: { minimum: 6 }
186
+
187
+ has_secure_password
188
+
189
+
190
+
191
+ # 与えられた文字列のハッシュ値を返す
192
+
193
+ def User.digest(string)
194
+
195
+ cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
196
+
197
+ BCrypt::Engine.cost
198
+
199
+ BCrypt::Password.create(string, cost: cost)
200
+
201
+ end
202
+
203
+
204
+
205
+ # ランダムなトークンを返す
206
+
207
+ def User.new_token
208
+
209
+ SecureRandom.urlsafe_base64
210
+
211
+ end
212
+
213
+ end
214
+
215
+ ```
216
+
217
+
218
+
219
+ user_test.rb
220
+
221
+
222
+
223
+ ```
224
+
225
+ require 'test_helper'
226
+
227
+
228
+
229
+ class UserTest < ActiveSupport::TestCase
230
+
231
+ # test "the truth" do
232
+
233
+ # assert true
234
+
235
+ # end
236
+
237
+ #@user.validはバリデーションを通るとture,invalidだと逆に通らないとtureになる
238
+
239
+
240
+
241
+ def setup
242
+
243
+ @user = User.new(name: 'foobar', email: 'sample@example.com', budget: 1000, exp:0, level:0, password:'foobar', password_confirmation: 'foobar')
244
+
245
+ end
246
+
247
+
248
+
249
+ #正しいユーザーはバリデーションを通る
250
+
251
+ test "user validates all" do
252
+
253
+ assert @user.valid?
254
+
255
+ end
256
+
257
+
258
+
259
+ #名前が50文字以上だと通らない
260
+
261
+ test "user validates name" do
262
+
263
+ @user.name = "a" * 51
264
+
265
+ assert @user.invalid?
266
+
267
+ end
268
+
269
+
270
+
271
+ #emailは正しいものを
272
+
273
+ test "user validates email" do
274
+
275
+ @user.email = "a"
276
+
277
+ assert @user.invalid?
278
+
279
+ end
280
+
281
+
282
+
283
+ #budgetは整数で
284
+
285
+ test "user validates budget" do
286
+
287
+ @user.budget = "100a"
288
+
289
+ assert @user.invalid?
290
+
291
+ end
292
+
293
+
294
+
295
+ #passwordは6文字以上
296
+
297
+ test "user validates password" do
298
+
299
+ @user.password = "a" * 5
300
+
301
+ @user.password_confirmation = @user.password
302
+
303
+ assert @user.invalid?
304
+
305
+ end
306
+
307
+
308
+
309
+ #passwodとpassword_confirmationは同じでなくてはいけない
310
+
311
+ test "user validates passowrd = password_confirmation" do
312
+
313
+ @user.password = "difference"
314
+
315
+ assert @user.invalid?
316
+
317
+ end
318
+
319
+
320
+
321
+ #登録されているemailは使えない(user.ymlのoneがtest@sample.com)
322
+
323
+ test "user validates email exists" do
324
+
325
+ @user.email = "test@sample.com"
326
+
327
+ assert @user.invalid?
328
+
329
+ end
330
+
331
+ end
332
+
333
+ ```

1

インデントのミス修正

2017/10/17 20:40

投稿

sakas1231
sakas1231

スコア42

test CHANGED
File without changes
test CHANGED
@@ -126,11 +126,11 @@
126
126
 
127
127
  flash[:success] = "User was successfully updated."
128
128
 
129
- format.html { redirect_to @user }
129
+ format.html { redirect_to @user }
130
130
 
131
- format.json { render :show, status: :ok, location: @user }
131
+ format.json { render :show, status: :ok, location: @user }
132
132
 
133
- return redirect_to user_url @user
133
+ return redirect_to user_url @user
134
134
 
135
135
  else
136
136
 
@@ -138,9 +138,9 @@
138
138
 
139
139
  flash[:notice] = "User updating was failed."
140
140
 
141
- format.html { render :edit }
141
+ format.html { render :edit }
142
142
 
143
- format.json { render json: @user.errors, status: :unprocessable_entity }
143
+ format.json { render json: @user.errors, status: :unprocessable_entity }
144
144
 
145
145
  end
146
146