質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Q&A

解決済

1回答

1976閲覧

rails rspecのエラー

YK282

総合スコア21

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

0グッド

0クリップ

投稿2019/08/11 09:54

前提・実現したいこと

現在、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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

siruku6

2019/08/11 10:44

migrationファイルも掲載した方がいいかもしれません。 ActiveModel::UnknownAttributeError: unknown attribute 'first_name' for User. ということなので、User モデルに first_name 列が存在しない可能性があります。 あとは、test 用の DBの状態とかが気になりますね。
guest

回答1

0

ベストアンサー

Everyday Rails - RSpecによるRailsテスト入門はRSpecの入門には良い本ですが、書かれた内容をそのまま入力して試すタイプの本ではないのでした。

サンプルコードのダウンロードのところに書いてあるように、https://github.com/everydayrails/everydayrails-rspec-2017 からソースを持ってきて、適切な章ごとのブランチをcheckoutして使います。

3. モデルスペックであれば、章の冒頭に、

この章で発生する変更点全体はGitHub上のこのdiffで確認できます。

最初から一緒にコードを書いていきたい場合は第1章の指示に従ってリポジトリをクローンし、それから1つ前の章のブランチからスタートしてください。

git checkout -b my-03-models origin/02-setup

という指示が書かれていますよね。これに従うとよいかと思います。

投稿2019/08/16 03:49

takahashim

総合スコア1877

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問