teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

文法の修正

2020/12/12 14:29

投稿

Flotsam
Flotsam

スコア11

title CHANGED
File without changes
body CHANGED
@@ -1,13 +1,13 @@
1
1
  前提・実現したいこと
2
2
  ---
3
- Ruby on rails新規登録機能を実装したい。
3
+ rails チュートリアル6章の新規登録機能を実装したい。
4
4
 
5
5
  発生している問題
6
6
  ---
7
- 実装するためspecを用いてテストを実行するもエラーが発生。
7
+ specを用いてテストを実行するもエラーが発生。
8
8
  テスト結果・エラーメッセージ
9
9
  ---
10
- ![イメージ説明](f6adced9850af7b926a855a1632f65a5.png)
10
+ ![イメージ説明](1b60caa979740bd514fe632330635541.png)
11
11
  該当のソースコード
12
12
  ---
13
13
  spec/models/user_spec.rb
@@ -15,39 +15,72 @@
15
15
  require 'rails_helper'
16
16
 
17
17
  RSpec.describe User, type: :model do
18
+ let(:user) { FactoryBot.create(:user) }
18
19
 
20
+ describe User do
21
+ # 有効なファクトリを持つこと
19
- it "名前とメールアドレスとパスワードがあれば登録できる" do
22
+ it "has a valid factory" do
20
- expect(FactoryBot.create(:user)).to be_valid
23
+ expect(user).to be_valid
24
+ end
21
25
  end
22
26
 
27
+ # Shoulda Matchers を使用
23
- it "名前がなければ登録できない" do
28
+ it { is_expected.to validate_presence_of :name }
29
+ it { is_expected.to validate_length_of(:name).is_at_most(50) }
30
+ it { is_expected.to validate_presence_of :email }
31
+ #it { is_expected.to validate_uniqueness_of(:email).case_insensitive } ・・・(*)
32
+ it { is_expected.to validate_length_of(:email).is_at_most(255) }
33
+ it { is_expected.to validate_presence_of :password }
24
- expect(FactoryBot.build(:user, name: "")).to_not be_valid
34
+ it { is_expected.to validate_length_of(:password).is_at_least(6) }
25
- end
26
35
 
27
- it "メールアドレスければ登録できい" do
36
+ # 重複したメールアドレスなら無効状態であること ・・・(*)
37
+ it "is invalid with a duplicate email adress" do
38
+ FactoryBot.create(:user, email: "aaron@example.com")
28
- expect(FactoryBot.build(:user, email: "")).to_not be_valid
39
+ user = FactoryBot.build(:user, email: "Aaron@example.com")
40
+ user.valid?
41
+ expect(user.errors[:email]).to include("has already been taken")
29
42
  end
30
43
 
44
+ # メールアドレスの有効性
45
+ describe "email validation should reject invalid addresses" do
46
+ # 無効なメールアドレスの場合
31
- it "メールアドレスが重複していたら登録できない" do
47
+ it "should be invalid" do
32
- user1 = FactoryBot.create(:user,name: "taro", email: "taro@example.com")
48
+ invalid_addresses = %w[user@example,com user_at_foo.org user.name@example.
49
+ foo@bar_baz.com foo@bar+baz.com]
50
+ invalid_addresses.each do |invalid_address|
51
+ user.email = invalid_address
52
+ expect(user).to_not be_valid
53
+ end
54
+ end
55
+
56
+ # 有効なメールアドレスの場合
57
+ it "should be valid" do
33
- expect(FactoryBot.build(:user, name: "ziro", email: user1.email)).to_not be_valid
58
+ valid_addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
59
+ valid_addresses.each do |valid_address|
60
+ user.email = valid_address
61
+ expect(user).to be_valid
62
+ end
63
+ end
34
64
  end
35
65
 
66
+ # パスワード確認が一致すること
67
+ describe "when password doesn't match confirmation" do
68
+ # 一致する場合
36
- it "パスワードがなければ登録できない" do
69
+ it "is valid when password confirmation matches password" do
37
- expect(FactoryBot.build(:user, password: "")).to_not be_valid
70
+ user = FactoryBot.build(:user, password: "password", password_confirmation: "password")
71
+ expect(user).to be_valid
72
+ end
73
+
74
+ # 一致しない場合
75
+ it "is invalid when password confirmation doesn't match password" do
76
+ user = FactoryBot.build(:user, password: "password", password_confirmation: "different")
77
+ user.valid?
78
+ expect(user.errors[:password_confirmation]).to include("doesn't match Password")
79
+ end
38
80
  end
39
81
  end
82
+
40
83
  ```
41
- spec/factories/user.rb
42
- ```ここに言語を入力
43
- FactoryBot.define do
44
- factory :user do
45
- name {"hiro"}
46
- sequence(:email) { |n| "hiro#{n}@example.com"}
47
- password {"password"}
48
- end
49
- end
50
- ```
51
84
  app/models/user.rb
52
85
  ```ここに言語を入力
53
86
  class User < ApplicationRecord
@@ -83,10 +116,14 @@
83
116
  end
84
117
  end
85
118
  ```
119
+ config/database.yml
120
+ ![イメージ説明](4b915f7b15c423c8d6b1c21e8aaa22ee.png)
86
121
  試したこと
87
122
  ---
88
- user.rbの中の「has_secure_password」を削除。
123
+ user.rbの中の「has_secure_password」を削除。
89
124
  ⇨ NoMethodError:
90
125
  undefined method `password=' for #<User:0x00005636d53dee08>というエラーが発生。
91
126
 
127
+ ・:Migration[5.2]を:Migration[5.0]へ変更
128
+
92
129
  モデル全て同じエラー文が返ってきているため、テスト文のミスと言うよりも設定絡みかと思うのですが、訂正のために時間を費やしてしまっているためアドバイスいただけないでしょうか。

1

誤字の修正

2020/12/12 14:29

投稿

Flotsam
Flotsam

スコア11

title CHANGED
File without changes
body CHANGED
@@ -58,7 +58,7 @@
58
58
  validates :email, presence: true, uniqueness: true
59
59
  end
60
60
  ```
61
- db/migrate/create_users.rb
61
+ db/migrate/
62
62
  ```ここに言語を入力
63
63
  class CreateUsers < ActiveRecord::Migration[5.2]
64
64
  def change
@@ -70,6 +70,18 @@
70
70
  end
71
71
  end
72
72
  end
73
+
74
+ class AddPasswordDigestToUsers < ActiveRecord::Migration[5.2]
75
+ def change
76
+ add_column :users, :password_digest, :string
77
+ end
78
+ end
79
+
80
+ class AddIndexToUsersEmail < ActiveRecord::Migration[5.2]
81
+ def change
82
+ add_index :users, :email, unique: true
83
+ end
84
+ end
73
85
  ```
74
86
  試したこと
75
87
  ---