前提・実現したいこと
Ruby on Rails Tutorial の6.3.3においてパスワードの最小文字設定をしようとすると以下のエラーメッセージが出てくる。
user.rbにvalidate :passwordを追加した途端エラーが起こるようになった。
ERRORの内容を調べたがわからないです。
自分でできることをやってみましたが解決できませんでした。
エラーの解決方法を知りたいです。
よろしくおねがいします。
発生している問題・エラーメッセージ
[1] guard(main)>
13:03:04 - INFO - Run all
13:03:04 - INFO - Running: all tests
Started with run options --seed 41877
FAIL["test_should_be_valid", UserTest, 0.20440399972721934]
test_should_be_valid#UserTest (0.20s)
Expected false to be truthy.
test/models/user_test.rb:15:in `block in class:UserTest'
FAIL["test_email_validation_should_accept_valid_addresses", UserTest, 0.22857500007376075]
test_email_validation_should_accept_valid_addresses#UserTest (0.23s)
"user@example.com" should be valid
test/models/user_test.rb:38:in block (2 levels) in <class:UserTest>' test/models/user_test.rb:36:in
each'
test/models/user_test.rb:36:in `block in class:UserTest'
ERROR["test_email_addresses_should_be_downcase", UserTest, 0.23315300000831485]
test_email_addresses_should_be_downcase#UserTest (0.23s)
ActiveRecord::RecordNotFound: ActiveRecord::RecordNotFound: Couldn't find User without an ID
test/models/user_test.rb:62:in `block in class:UserTest'
15/15: [========================================] 100% Time: 00:00:00, Time: 00:00:00
Finished in 0.59527s
15 tests, 29 assertions, 2 failures, 1 errors, 0 skips
/Users/myname/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/guard-2.13.0/lib/guard/jobs/pry_wrapper.rb:279: warning: method Pry#input_array is deprecated. Use Pry#input_ring instead
該当のソースコード
user.rb
class User < ApplicationRecord before_save { self.email = email.downcase } 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} has_secure_password validates :password, presence: true, length: { minimum: 6 } end user_test.rb require 'test_helper' class UserTest < ActiveSupport::TestCase # test "the truth" do # assert true # end def setup @user = User.new(name: "Chosu Riki", email:"kuttemina@tobuzo.com", password: "chosu", password_confirmation: "chosu") end test "should be valid" do @user.save assert @user.valid? end test "name should be present" do @user.name = " " assert_not @user.valid? end test "name should not be too long" do @user.name = "a" * 51 assert_not @user.valid? end #email validation section test "email should not be too long" do @user.email = "a" * 244 + "@tobuzoooo.com" assert_not @user.valid? end test "email validation should accept valid addresses" do valid_addresses = %w[user@example.com USER@foo.COM A_DHDH@foo.bar.org first.last@foo.jp alice_bob@boz.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.jp user.name@example. foo@bar_baz.com foo@bar+bar.com user@foo..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 test "email addresses should be downcase" do random_case_email = "FOo@exAMple.Com" @user.email = random_case_email @user.save assert_equal random_case_email.downcase, @user.reload.email end #password validation section test "password should be present (nonblank)" do @user.password = @user.password_confirmation = " " * 6 assert_not @user.valid? end test "password should have a minimum length" do @user.password = @user.password_confirmation = "a" * 5 assert_not @user.valid? end end ```ここに言語名を入力 Ruby ソースコード
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー