前提・実現したいこと
現在、Rspecの練習としてRspecの入門というサイトのRspec構文というところまで進めました。
しかし、全く同じ通りに進めているのですが、first_nameがUserにないとエラーが出てしまいます。
どなたかわかる方がいらっしゃいましたらご教授いただけると幸いです。
発生している問題・エラーメッセージ
ec2-user:~/environment/s (master) $ bin/rspec Running via Spring preloader in process 10715 User is valid with a first name, last name, email, and password (FAILED - 1) is invalid without a first name (PENDING: Not yet implemented) is invalid without a last name (PENDING: Not yet implemented) is invalid without an email address (PENDING: Not yet implemented) is invalid with a duplicate email address (PENDING: Not yet implemented) returns a user's full name as a string (PENDING: Not yet implemented) Pending: (Failures listed here are expected and do not affect your suite's status) 1) User is invalid without a first name # Not yet implemented # ./spec/models/user_spec.rb:16 2) User is invalid without a last name # Not yet implemented # ./spec/models/user_spec.rb:18 3) User is invalid without an email address # Not yet implemented # ./spec/models/user_spec.rb:20 4) User is invalid with a duplicate email address # Not yet implemented # ./spec/models/user_spec.rb:22 5) User returns a user's full name as a string # Not yet implemented # ./spec/models/user_spec.rb:24 Failures: 1) User is valid with a first name, last name, email, and password Failure/Error: user = User.new( first_name: "Aaron", last_name: "Sumner", email: "tester@example.com", password: "dottle-nouveau-pavilion-tights-furze", ) ActiveModel::UnknownAttributeError: unknown attribute 'first_name' for User. # ./spec/models/user_spec.rb:6:in `block (2 levels) in <main>' # /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load' # /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:54:in `load' # /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call' # -e:1:in `<main>' Finished in 0.0169 seconds (files took 0.1511 seconds to load) 6 examples, 1 failure, 5 pending Failed examples: rspec ./spec/models/user_spec.rb:5 # User is valid with a first name, last name, email, and password
該当のソースコード
user_spec.rb
require 'rails_helper' RSpec.describe User, type: :model do # 姓、名、メール、パスワードがあれば有効な状態であること it "is valid with a first name, last name, email, and password" do user = User.new( first_name: "Aaron", last_name: "Sumner", email: "tester@example.com", password: "dottle-nouveau-pavilion-tights-furze", ) expect(user).to be_valid end # 名がなければ無効な状態であること it "is invalid without a first name" # 姓がなければ無効な状態であること it "is invalid without a last name" # メールアドレスがなければ無効な状態であること it "is invalid without an email address" # 重複したメールアドレスなら無効な状態であること it "is invalid with a duplicate email address" # ユーザーのフルネームを文字列として返すこと it "returns a user's full name as a string" end
補足情報(FW/ツールのバージョンなど)
rails5.2.3
rspec3.6.0
migrationファイルも掲載した方がいいかもしれません。
ActiveModel::UnknownAttributeError: unknown attribute 'first_name' for User.
ということなので、User モデルに first_name 列が存在しない可能性があります。
あとは、test 用の DBの状態とかが気になりますね。
回答1件
あなたの回答
tips
プレビュー