質問編集履歴
2
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -226,9 +226,9 @@
|
|
226
226
|
|
227
227
|
```
|
228
228
|
|
229
|
-
|
229
|
+
user.test.rb
|
230
|
-
|
230
|
+
|
231
|
-
``
|
231
|
+
```
|
232
232
|
|
233
233
|
require 'test_helper'
|
234
234
|
|
1
コードの追加
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Railsチュートリアル9章 エラー
|
1
|
+
Railsチュートリアル9章 BCryptエラー、ActionViewTemplateエラー
|
test
CHANGED
@@ -2,10 +2,390 @@
|
|
2
2
|
|
3
3
|
エラーが三つある状態です。
|
4
4
|
|
5
|
-
|
5
|
+
|
6
6
|
|
7
7
|
1つめはUserのmodelの中に保存されているデータの保存のされ方の問題という風に調べると出てきたのでユーザーをリセットし、もう一度登録したのですが、何も変わらない状態です。
|
8
8
|
|
9
9
|
また。2、3こめのActionview:Template:Errorは予期せぬ場所に飛ばされているという意味だと理解しているのですが、初学なので全てコピーアンドペーストでやってきたので何が悪いのかわからない状態です。
|
10
10
|
|
11
11
|
勉強し始めなので、質問の仕方など、至らない点も多いと思いますが、よろしくお願いいたします。
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
ターミナル
|
16
|
+
|
17
|
+
```Rubyec2-user:~/environment/insta_app (advanced-login) $ rails test
|
18
|
+
|
19
|
+
Running via Spring preloader in process 6852
|
20
|
+
|
21
|
+
Run options: --seed 29218
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
# Running:
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
...E
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
Error:
|
34
|
+
|
35
|
+
UserTest#test_authenticated?_should_return_false_for_a_user_with_nil_digest:
|
36
|
+
|
37
|
+
BCrypt::Errors::InvalidHash: invalid hash
|
38
|
+
|
39
|
+
app/models/user.rb:32:in `new'
|
40
|
+
|
41
|
+
app/models/user.rb:32:in `authenticated?'
|
42
|
+
|
43
|
+
test/models/user_test.rb:77:in `block in <class:UserTest>'
|
44
|
+
|
45
|
+
bin/rails test test/models/user_test.rb:76
|
46
|
+
|
47
|
+
.........E
|
48
|
+
|
49
|
+
Error:
|
50
|
+
|
51
|
+
UsersEditTest#test_successful_edit:
|
52
|
+
|
53
|
+
ActionView::Template::Error: First argument in form cannot contain nil or be empty
|
54
|
+
|
55
|
+
app/views/users/edit.html.erb:6:in `_app_views_users_edit_html_erb___280578002934169021_45554900'
|
56
|
+
|
57
|
+
test/integration/users_edit_test.rb:24:in `block in <class:UsersEditTest>'
|
58
|
+
|
59
|
+
bin/rails test test/integration/users_edit_test.rb:22
|
60
|
+
|
61
|
+
E
|
62
|
+
|
63
|
+
Error:
|
64
|
+
|
65
|
+
UsersEditTest#test_unsuccessful_edit:
|
66
|
+
|
67
|
+
ActionView::Template::Error: First argument in form cannot contain nil or be empty
|
68
|
+
|
69
|
+
app/views/users/edit.html.erb:6:in `_app_views_users_edit_html_erb___280578002934169021_45554900'
|
70
|
+
|
71
|
+
test/integration/users_edit_test.rb:11:in `block in <class:UsersEditTest>'
|
72
|
+
|
73
|
+
bin/rails test test/integration/users_edit_test.rb:9
|
74
|
+
|
75
|
+
..............
|
76
|
+
|
77
|
+
Finished in 0.641378s, 45.2151 runs/s, 106.0217 assertions/s.
|
78
|
+
|
79
|
+
29 runs, 68 assertions, 0 failures, 3 errors, 0 skips
|
80
|
+
|
81
|
+
```
|
82
|
+
|
83
|
+
edit.html.erb
|
84
|
+
|
85
|
+
```
|
86
|
+
|
87
|
+
<% provide(:title, "Edit user") %>
|
88
|
+
|
89
|
+
<h1>Update your profile</h1>
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
<div class="row">
|
94
|
+
|
95
|
+
<div class="col-md-6 col-md-offset-3">
|
96
|
+
|
97
|
+
<%= form_for(@user) do |f| %>
|
98
|
+
|
99
|
+
<%= render 'shared/error_messages' %>
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
<%= f.label :name %>
|
104
|
+
|
105
|
+
<%= f.text_field :name, class: 'form-control' %>
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
<%= f.label :email %>
|
110
|
+
|
111
|
+
<%= f.email_field :email, class: 'form-control' %>
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
<%= f.label :password %>
|
116
|
+
|
117
|
+
<%= f.password_field :password, class: 'form-control' %>
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
<%= f.label :password_confirmation, "Confirmation" %>
|
122
|
+
|
123
|
+
<%= f.password_field :password_confirmation, class: 'form-control' %>
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
<%= f.submit "Save changes", class: "btn btn-primary" %>
|
128
|
+
|
129
|
+
<% end %>
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
<div class="gravatar_edit">
|
134
|
+
|
135
|
+
<%= gravatar_for @user %>
|
136
|
+
|
137
|
+
<a href="http://gravatar.com/emails" target="_blank">change</a>
|
138
|
+
|
139
|
+
</div>
|
140
|
+
|
141
|
+
</div>
|
142
|
+
|
143
|
+
</div>
|
144
|
+
|
145
|
+
```
|
146
|
+
|
147
|
+
user.edit.test.rb
|
148
|
+
|
149
|
+
```
|
150
|
+
|
151
|
+
require 'test_helper'
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
class UsersEditTest < ActionDispatch::IntegrationTest
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
def setup
|
160
|
+
|
161
|
+
@user = users(:michael)
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
test "unsuccessful edit" do
|
168
|
+
|
169
|
+
log_in_as(@user)
|
170
|
+
|
171
|
+
get edit_user_path(@user)
|
172
|
+
|
173
|
+
assert_template 'users/edit'
|
174
|
+
|
175
|
+
patch user_path(@user), params: { user: { name: "",
|
176
|
+
|
177
|
+
email: "foo@invalid",
|
178
|
+
|
179
|
+
password: "foo",
|
180
|
+
|
181
|
+
password_confirmation: "bar" } }
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
assert_template 'users/edit'
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
test "successful edit" do
|
194
|
+
|
195
|
+
log_in_as(@user)
|
196
|
+
|
197
|
+
get edit_user_path(@user)
|
198
|
+
|
199
|
+
assert_redirected_to edit_user_url(@user)
|
200
|
+
|
201
|
+
name = "Foo Bar"
|
202
|
+
|
203
|
+
email = "foo@bar.com"
|
204
|
+
|
205
|
+
patch user_path(@user), params: { user: { name: name,
|
206
|
+
|
207
|
+
email: email,
|
208
|
+
|
209
|
+
password: "",
|
210
|
+
|
211
|
+
password_confirmation: "" } }
|
212
|
+
|
213
|
+
assert_not flash.empty?
|
214
|
+
|
215
|
+
assert_redirected_to @user
|
216
|
+
|
217
|
+
@user.reload
|
218
|
+
|
219
|
+
assert_equal name, @user.name
|
220
|
+
|
221
|
+
assert_equal email, @user.email
|
222
|
+
|
223
|
+
end
|
224
|
+
|
225
|
+
end
|
226
|
+
|
227
|
+
```
|
228
|
+
|
229
|
+
`user.test.rb
|
230
|
+
|
231
|
+
``
|
232
|
+
|
233
|
+
require 'test_helper'
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
class UserTest < ActiveSupport::TestCase
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
def setup
|
242
|
+
|
243
|
+
@user = User.new(name: "Example User", email: "user@example.com",
|
244
|
+
|
245
|
+
password: "foobar", password_confirmation: "foobar")
|
246
|
+
|
247
|
+
end
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
test "should be valid" do
|
252
|
+
|
253
|
+
assert @user.valid?
|
254
|
+
|
255
|
+
end
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
test "name should be present" do
|
260
|
+
|
261
|
+
@user.name = " "
|
262
|
+
|
263
|
+
assert_not @user.valid?
|
264
|
+
|
265
|
+
end
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
test "email should be present" do
|
270
|
+
|
271
|
+
@user.email = " "
|
272
|
+
|
273
|
+
assert_not @user.valid?
|
274
|
+
|
275
|
+
end
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
test "name should not be too long" do
|
280
|
+
|
281
|
+
@user.name = "a" * 51
|
282
|
+
|
283
|
+
assert_not @user.valid?
|
284
|
+
|
285
|
+
end
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
test "email should not be too long" do
|
290
|
+
|
291
|
+
@user.email = "a" * 244 + "@example.com"
|
292
|
+
|
293
|
+
assert_not @user.valid?
|
294
|
+
|
295
|
+
end
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
test "email validation should accept valid addresses" do
|
300
|
+
|
301
|
+
valid_addresses = %w[user@example.com USER@foo.COM A_US-ER@foo.bar.org
|
302
|
+
|
303
|
+
first.last@foo.jp alice+bob@baz.cn]
|
304
|
+
|
305
|
+
valid_addresses.each do |valid_address|
|
306
|
+
|
307
|
+
@user.email = valid_address
|
308
|
+
|
309
|
+
assert @user.valid?, "#{valid_address.inspect} should be valid"
|
310
|
+
|
311
|
+
end
|
312
|
+
|
313
|
+
end
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
test "email validation should reject invalid addresses" do
|
318
|
+
|
319
|
+
invalid_addresses = %w[user@example,com user_at_foo.org user.name@example.
|
320
|
+
|
321
|
+
foo@bar_baz.com foo@bar+baz.com]
|
322
|
+
|
323
|
+
invalid_addresses.each do |invalid_address|
|
324
|
+
|
325
|
+
@user.email = invalid_address
|
326
|
+
|
327
|
+
assert_not @user.valid?, "#{invalid_address.inspect} should be invalid"
|
328
|
+
|
329
|
+
end
|
330
|
+
|
331
|
+
end
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
test "email addresses should be unique" do
|
336
|
+
|
337
|
+
duplicate_user = @user.dup
|
338
|
+
|
339
|
+
duplicate_user.email = @user.email.upcase
|
340
|
+
|
341
|
+
@user.save
|
342
|
+
|
343
|
+
assert_not duplicate_user.valid?
|
344
|
+
|
345
|
+
end
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
test "email addresses should be saved as lower-case" do
|
350
|
+
|
351
|
+
mixed_case_email = "Foo@ExAMPle.CoM"
|
352
|
+
|
353
|
+
@user.email = mixed_case_email
|
354
|
+
|
355
|
+
@user.save
|
356
|
+
|
357
|
+
assert_equal mixed_case_email.downcase, @user.reload.email
|
358
|
+
|
359
|
+
end
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
test "password should be present (nonblank)" do
|
364
|
+
|
365
|
+
@user.password = @user.password_confirmation = " " * 6
|
366
|
+
|
367
|
+
assert_not @user.valid?
|
368
|
+
|
369
|
+
end
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
test "password should have a minimum length" do
|
374
|
+
|
375
|
+
@user.password = @user.password_confirmation = "a" * 5
|
376
|
+
|
377
|
+
assert_not @user.valid?
|
378
|
+
|
379
|
+
end
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
test "authenticated? should return false for a user with nil digest" do
|
384
|
+
|
385
|
+
assert_not @user.authenticated?('')
|
386
|
+
|
387
|
+
end
|
388
|
+
|
389
|
+
end
|
390
|
+
|
391
|
+
```
|