Error: UserTest#test_email_addresses_should_be_unique: ActiveModel::UnknownAttributeError: unknown attribute 'password' for User.のエラーについて
Q&A
Closed
解決したいこと
railsチュートリアル6章を学習中にrails testを実行すると8つエラーが出ました。
解決したいのでよろしくお願いします。
発生している問題・エラー
Error:
UserTest#test_email_validation_should_accept_valid_addresses:
ActiveModel::UnknownAttributeError: unknown attribute 'password' for User.
test/models/user_test.rb:6:in `setup'
rails test test/models/user_test.rb:34
E
Error:
UserTest#test_should_be_valid:
ActiveModel::UnknownAttributeError: unknown attribute 'password' for User.
test/models/user_test.rb:6:in `setup'
rails test test/models/user_test.rb:10
E
Error:
UserTest#test_email_should_not_be_too_long:
ActiveModel::UnknownAttributeError: unknown attribute 'password' for User.
test/models/user_test.rb:6:in `setup'
rails test test/models/user_test.rb:29
E
Error:
UserTest#test_email_validation_should_reject_invalid_addresses:
ActiveModel::UnknownAttributeError: unknown attribute 'password' for User.
test/models/user_test.rb:6:in `setup'
rails test test/models/user_test.rb:43
E
Error:
UserTest#test_name_should_not_be_too_long:
ActiveModel::UnknownAttributeError: unknown attribute 'password' for User.
test/models/user_test.rb:6:in `setup'
rails test test/models/user_test.rb:24
E
Error:
UserTest#test_email_should_be_present:
ActiveModel::UnknownAttributeError: unknown attribute 'password' for User.
test/models/user_test.rb:6:in `setup'
rails test test/models/user_test.rb:19
E
Error:
UserTest#test_name_should_be_present:
ActiveModel::UnknownAttributeError: unknown attribute 'password' for User.
test/models/user_test.rb:6:in `setup'
rails test test/models/user_test.rb:14
E
Error:
UserTest#test_email_addresses_should_be_unique:
ActiveModel::UnknownAttributeError: unknown attribute 'password' for User.
test/models/user_test.rb:6:in `setup'
rails test test/models/user_test.rb:52
.......
Finished in 2.014780s, 7.4450 runs/s, 8.4376 assertions/s.
15 runs, 17 assertions, 0 failures, 8 errors, 0 skips
ソースコード
test/models/user_test.rb
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
@user = User.new(name: "Example User", email: "user@example.com",
password: "foobar", password_confirmation: "foobar")
end
test "should be valid" do
assert @user.valid?
end
test "name should be present" do
@user.name = " "
assert_not @user.valid?
end
test "email should be present" do
@user.email = " "
assert_not @user.valid?
end
test "name should not be too long" do
@user.name = "a" * 51
assert_not @user.valid?
end
test "email should not be too long" do
@user.email = "a" * 244 + "@example.com"
assert_not @user.valid?
end
test "email validation should accept valid addresses" do
valid_addresses = %w[user@example.com USER@foo.COM A_US-ER@foo.bar.org
first.last@foo.jp alice+bob@baz.cn]
valid_addresses.each do |valid_address|
@user.email = valid_address
assert @user.valid?, "#{valid_address.inspect} should be valid"
end
end
test "email validation should reject invalid addresses" do
invalid_addresses = %w[user@example,com user_at_foo.org user.name@example.
foo@bar_baz.com foo@bar+baz.com]
invalid_addresses.each do |invalid_address|
@user.email = invalid_address
assert_not @user.valid?, "#{invalid_address.inspect} should be invalid"
end
end
test "email addresses should be unique" do
duplicate_user = @user.dup
duplicate_user.email = @user.email.upcase
@user.save
assert_not duplicate_user.valid?
end
end
app/models/user.rb
class User < ApplicationRecord
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
end
自分で試したこと
Gemfileにbcryptを追加しbundle installした。
よろしくお願いします。
0