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

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

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

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

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

RSpec

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

Q&A

1回答

639閲覧

[Devise][RSpec]NoMethodError:undefined method 'password' in 'user' factoryの原因

yokoyanmo

総合スコア12

Ruby on Rails 5

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

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

RSpec

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

0グッド

0クリップ

投稿2019/02/26 07:06

編集2019/02/26 14:09

事態

Deviseを用いたRailsアプリを開発しています。
RSpecを用いてまずはUserのモデルスペックを作成しているところです。
FactoryBotをインストールし、UserのFactoryを作成したうえでRSpecを実行したのですが掲題のエラーが発生します。
(エラーの詳細や関連ファイルは下記にあります。)
エラー内容でググったり調べてみましたが、解決策や解決への手がかりが見つけられず、お手数ですがご教示頂けますと幸いです。
どうぞよろしくお願い致します。
必要なファイル内容等あればその旨ご連絡ください。

###環境
ruby : 2.5.1
rails : 5.2.2
device : 4.6.1

発生している問題・エラーメッセージ

$ bundle exec rspec spec/models/user_spec.rb An error occurred while loading ./spec/models/user_spec.rb. Failure/Error: password "password" NoMethodError: undefined method 'password' in 'user' factory # ./spec/factories/users.rb:5:in `block (2 levels) in <main>' # ./spec/factories/users.rb:2:in `block in <main>' # ./spec/factories/users.rb:1:in `<main>' # ./config/environment.rb:5:in `<top (required)>' # ./spec/rails_helper.rb:4:in `require' # ./spec/rails_helper.rb:4:in `<top (required)>' # ./spec/models/user_spec.rb:1:in `require' # ./spec/models/user_spec.rb:1:in `<top (required)>' No examples found. Finished in 0.00064 seconds (files took 2.23 seconds to load) 0 examples, 0 failures, 1 error occurred outside of examples

ruby

1# spec/factories/users.rb 2FactoryBot.define do 3 factory :user do 4 sequence(:name) { |n| "User#{n}" } 5 sequence(:email) { |n| "tester#{n}@example.com" } 6 password "password" 7 password_confirmation "password" 8 end 9end

ruby

1# spec/models/user_spec.rb 2require 'rails_helper' 3 4RSpec.describe User, type: :model do 5 it "has a valid user" do 6 expect(build(:user)).to be_valid 7 end 8end

ruby

1# app/models/user.rb 2class User < ApplicationRecord 3 has_many :posts, dependent: :destroy 4 has_many :likes 5 has_many :comments 6 # Include default devise modules. Others available are: 7 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 8 devise :database_authenticatable, :registerable, 9 :recoverable, :rememberable, :validatable 10 11 validates :name, presence: true, length: { maximum: 50 } 12 13 def update_without_current_password(params, *options) 14 params.delete(:current_password) 15 16 if params[:password].blank? && params[:password_confirmation].blank? 17 params.delete(:password) 18 params.delete(:password_confirmation) 19 end 20 21 result = update_attributes(params, *options) 22 clean_up_passwords 23 result 24 end 25end

ruby

1# db/schema.rb 2create_table "users", force: :cascade do |t| 3 t.string "email", default: "", null: false 4 t.string "encrypted_password", default: "", null: false 5 t.string "reset_password_token" 6 t.datetime "reset_password_sent_at" 7 t.datetime "remember_created_at" 8 t.datetime "created_at", null: false 9 t.datetime "updated_at", null: false 10 t.string "name", default: "", null: false 11 t.string "profile_photo" 12 t.string "website" 13 t.text "bio" 14 t.index ["email"], name: "index_users_on_email", unique: true 15 t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

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

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

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

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

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

guest

回答1

0

自己解決しました。
参考にしているEverydayrailsという書籍やRSpec関連のQiitaを見ても必ずしも以下の記法でなくても問題なさそうで原因が不明でありこれが正しい解決方法なのかは分かりませんが、spec/factories/users.rbのpasswordに以下のように{}を付けたところ正常に動きました。
自分でも調べますが、もし原因がパッと分かる方、可能性としてこれじゃないかとお考えがある方がいらっしゃいましたらご教示頂けますと幸いです。

ruby

1FactoryBot.define do 2 factory :user do 3 sequence(:name) { |n| "User - #{n}" } 4 sequence(:email) { |n| "tester#{n}@example.com" } 5 password { "password" } 6 password_confirmation { "password" } 7 end 8end

投稿2019/02/26 14:05

yokoyanmo

総合スコア12

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問