質問編集履歴

2

文法の修正

2020/12/12 14:29

投稿

Flotsam
Flotsam

スコア11

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ---
4
4
 
5
- Ruby on rails新規登録機能を実装したい。
5
+ rails チュートリアル6章の新規登録機能を実装したい。
6
6
 
7
7
 
8
8
 
@@ -10,13 +10,13 @@
10
10
 
11
11
  ---
12
12
 
13
- 実装するためspecを用いてテストを実行するもエラーが発生。
13
+ specを用いてテストを実行するもエラーが発生。
14
14
 
15
15
  テスト結果・エラーメッセージ
16
16
 
17
17
  ---
18
18
 
19
- ![イメージ説明](f6adced9850af7b926a855a1632f65a5.png)
19
+ ![イメージ説明](1b60caa979740bd514fe632330635541.png)
20
20
 
21
21
  該当のソースコード
22
22
 
@@ -32,96 +32,162 @@
32
32
 
33
33
  RSpec.describe User, type: :model do
34
34
 
35
-
36
-
37
- it "名前とメールアドレスとパスワードがあれば登録できる" do
38
-
39
- expect(FactoryBot.create(:user)).to be_valid
40
-
41
- end
42
-
43
-
44
-
45
- it "名前がなければ登録できない" do
46
-
47
- expect(FactoryBot.build(:user, name: "")).to_not be_valid
48
-
49
- end
50
-
51
-
52
-
53
- it "メールアドレスがなければ登録できない" do
54
-
55
- expect(FactoryBot.build(:user, email: "")).to_not be_valid
56
-
57
- end
58
-
59
-
60
-
61
- it "メールアドレスが重複していたら登録できない" do
62
-
63
- user1 = FactoryBot.create(:user,name: "taro", email: "taro@example.com")
64
-
65
- expect(FactoryBot.build(:user, name: "ziro", email: user1.email)).to_not be_valid
66
-
67
- end
68
-
69
-
70
-
71
- it "パスワードければ登録きない" do
72
-
73
- expect(FactoryBot.build(:user, password: "")).to_not be_valid
74
-
75
- end
76
-
77
- end
78
-
79
- ```
80
-
81
- spec/factories/user.rb
35
+ let(:user) { FactoryBot.create(:user) }
36
+
37
+
38
+
39
+ describe User do
40
+
41
+ # 有効なファクトリを持つこと
42
+
43
+ it "has a valid factory" do
44
+
45
+ expect(user).to be_valid
46
+
47
+ end
48
+
49
+ end
50
+
51
+
52
+
53
+ # Shoulda Matchers を使用
54
+
55
+ it { is_expected.to validate_presence_of :name }
56
+
57
+ it { is_expected.to validate_length_of(:name).is_at_most(50) }
58
+
59
+ it { is_expected.to validate_presence_of :email }
60
+
61
+ #it { is_expected.to validate_uniqueness_of(:email).case_insensitive } ・・・(*)
62
+
63
+ it { is_expected.to validate_length_of(:email).is_at_most(255) }
64
+
65
+ it { is_expected.to validate_presence_of :password }
66
+
67
+ it { is_expected.to validate_length_of(:password).is_at_least(6) }
68
+
69
+
70
+
71
+ # 重複したメルアレスら無効な状態あること ・・・(*)
72
+
73
+ it "is invalid with a duplicate email adress" do
74
+
75
+ FactoryBot.create(:user, email: "aaron@example.com")
76
+
77
+ user = FactoryBot.build(:user, email: "Aaron@example.com")
78
+
79
+ user.valid?
80
+
81
+ expect(user.errors[:email]).to include("has already been taken")
82
+
83
+ end
84
+
85
+
86
+
87
+ # メールアドレスの有効性
88
+
89
+ describe "email validation should reject invalid addresses" do
90
+
91
+ # 無効なメールアドレスの場合
92
+
93
+ it "should be invalid" do
94
+
95
+ invalid_addresses = %w[user@example,com user_at_foo.org user.name@example.
96
+
97
+ foo@bar_baz.com foo@bar+baz.com]
98
+
99
+ invalid_addresses.each do |invalid_address|
100
+
101
+ user.email = invalid_address
102
+
103
+ expect(user).to_not be_valid
104
+
105
+ end
106
+
107
+ end
108
+
109
+
110
+
111
+ # 有効なメールアドレスの場合
112
+
113
+ it "should be valid" do
114
+
115
+ valid_addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
116
+
117
+ valid_addresses.each do |valid_address|
118
+
119
+ user.email = valid_address
120
+
121
+ expect(user).to be_valid
122
+
123
+ end
124
+
125
+ end
126
+
127
+ end
128
+
129
+
130
+
131
+ # パスワード確認が一致すること
132
+
133
+ describe "when password doesn't match confirmation" do
134
+
135
+ # 一致する場合
136
+
137
+ it "is valid when password confirmation matches password" do
138
+
139
+ user = FactoryBot.build(:user, password: "password", password_confirmation: "password")
140
+
141
+ expect(user).to be_valid
142
+
143
+ end
144
+
145
+
146
+
147
+ # 一致しない場合
148
+
149
+ it "is invalid when password confirmation doesn't match password" do
150
+
151
+ user = FactoryBot.build(:user, password: "password", password_confirmation: "different")
152
+
153
+ user.valid?
154
+
155
+ expect(user.errors[:password_confirmation]).to include("doesn't match Password")
156
+
157
+ end
158
+
159
+ end
160
+
161
+ end
162
+
163
+
164
+
165
+ ```
166
+
167
+ app/models/user.rb
82
168
 
83
169
  ```ここに言語を入力
84
170
 
85
- FactoryBot.define do
86
-
87
- factory :user do
88
-
89
- name {"hiro"}
171
+ class User < ApplicationRecord
90
-
91
- sequence(:email) { |n| "hiro#{n}@example.com"}
172
+
92
-
93
- password {"password"}
173
+ has_secure_password
174
+
175
+
176
+
94
-
177
+ validates :name, presence: true
178
+
179
+ validates :password, presence: true
180
+
181
+ validates :email, presence: true, uniqueness: true
182
+
95
- end
183
+ end
96
-
97
- end
184
+
98
-
99
- ```
185
+ ```
100
-
186
+
101
- app/models/user.rb
187
+ db/migrate/
102
188
 
103
189
  ```ここに言語を入力
104
190
 
105
- class User < ApplicationRecord
106
-
107
- has_secure_password
108
-
109
-
110
-
111
- validates :name, presence: true
112
-
113
- validates :password, presence: true
114
-
115
- validates :email, presence: true, uniqueness: true
116
-
117
- end
118
-
119
- ```
120
-
121
- db/migrate/
122
-
123
- ```ここに言語を入力
124
-
125
191
  class CreateUsers < ActiveRecord::Migration[5.2]
126
192
 
127
193
  def change
@@ -168,11 +234,15 @@
168
234
 
169
235
  ```
170
236
 
237
+ config/database.yml
238
+
239
+ ![イメージ説明](4b915f7b15c423c8d6b1c21e8aaa22ee.png)
240
+
171
241
  試したこと
172
242
 
173
243
  ---
174
244
 
175
- user.rbの中の「has_secure_password」を削除。
245
+ user.rbの中の「has_secure_password」を削除。
176
246
 
177
247
  ⇨ NoMethodError:
178
248
 
@@ -180,4 +250,8 @@
180
250
 
181
251
 
182
252
 
253
+ ・:Migration[5.2]を:Migration[5.0]へ変更
254
+
255
+
256
+
183
257
  モデル全て同じエラー文が返ってきているため、テスト文のミスと言うよりも設定絡みかと思うのですが、訂正のために時間を費やしてしまっているためアドバイスいただけないでしょうか。

1

誤字の修正

2020/12/12 14:29

投稿

Flotsam
Flotsam

スコア11

test CHANGED
File without changes
test CHANGED
@@ -118,7 +118,7 @@
118
118
 
119
119
  ```
120
120
 
121
- db/migrate/create_users.rb
121
+ db/migrate/
122
122
 
123
123
  ```ここに言語を入力
124
124
 
@@ -142,6 +142,30 @@
142
142
 
143
143
  end
144
144
 
145
+
146
+
147
+ class AddPasswordDigestToUsers < ActiveRecord::Migration[5.2]
148
+
149
+ def change
150
+
151
+ add_column :users, :password_digest, :string
152
+
153
+ end
154
+
155
+ end
156
+
157
+
158
+
159
+ class AddIndexToUsersEmail < ActiveRecord::Migration[5.2]
160
+
161
+ def change
162
+
163
+ add_index :users, :email, unique: true
164
+
165
+ end
166
+
167
+ end
168
+
145
169
  ```
146
170
 
147
171
  試したこと