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

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

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

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

RSpec

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

Ruby on Rails

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

Q&A

0回答

1764閲覧

Rails開発中のRSpec・FactoryBot利用時のエラー

j30st

総合スコア28

Ruby on Rails 5

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

RSpec

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/03/28 04:18

前提・実現したいこと

開発中のRailsアプリ内に実装しているフォロー機能に関して、RSpec及びFactoryBotを使用して、モデルテストを行っているのですがテストを通過できず困っています。

該当のソースコード

  • Usersテーブル

ruby

1class DeviseCreateUsers < ActiveRecord::Migration[5.2] 2 def change 3 create_table :users do |t| 4 t.string :nickname, null: false 5 t.binary :image 6 t.string :email, null: false, default: "" 7 t.string :workplace 8 t.string :encrypted_password, null: false, default: "" 9 t.string :reset_password_token 10 t.datetime :reset_password_sent_at 11 t.datetime :remember_created_at 12 t.timestamps null: false 13 end 14 15 add_index :users, :email, unique: true 16 add_index :users, :reset_password_token, unique: true 17 end 18end
  • Relationshipsテーブル

ruby

1class CreateRelationships < ActiveRecord::Migration[5.2] 2 def change 3 create_table :relationships do |t| 4 t.references :user, foreign_key: true 5 t.references :follow, foreign_key: { to_table: :users } 6 t.timestamps 7 t.index [:user_id, :follow_id], unique: true 8 end 9 end 10end
  • Relationshipモデル

ruby

1class Relationship < ApplicationRecord 2 belongs_to :follower, class_name: 'User', foreign_key: 'user_id' 3 belongs_to :followed, class_name: 'User', foreign_key: 'follow_id' 4 5 validates :user_id, presence: true 6 validates :follow_id, presence: true 7end
  • Userモデル

アプリ内の便宜上、
user_idをfollower
follow_idをfollowed
とし、使用しています。

ruby

1class User < ApplicationRecord 2 #ユーザーとフォローする人(follower)を結びつける 3 has_many :follower, class_name: 'Relationship', foreign_key: 'user_id' 4 #ユーザーとフォローされる人(followed)を結びつける 5 has_many :followed, class_name: 'Relationship', foreign_key: 'follow_id' 6 7 #自分がフォローしているユーザーを取得する 8 has_many :following_user, through: :follower, source: :followed 9 #自分をフォローしているユーザーを取得する 10 has_many :follower_user, through: :followed, source: :follower 11end
  • factory_bot

ruby

1FactoryBot.define do 2 3 factory :relationship do 4 user_id {1} 5 follow_id {2} 6 end 7 8end
  • specファイル

ruby

1require 'rails_helper' 2 3describe Relationship do 4 describe '#create' do 5 context 'can save' do 6 7 it 'user_idとfollow_idがあれば登録できる' do 8 relationship = build(:relationship) 9 expect(relationship).to be_valid 10 end 11 12 end 13 end 14end

$bundle exec rspecを実行

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

1) Relationship#create can save user_idとfollow_idがあれば登録できる Failure/Error: expect(relationship).to be_valid expected #<Relationship id: nil, user_id: 1, follow_id: 2, created_at: nil, updated_at: nil> to be valid, but got errors: Follower must exist, Followed must exist # ./spec/models/relationship_spec.rb:9:in `block (4 levels) in <top (required)>' Finished in 0.57046 seconds (files took 4.63 seconds to load) 1 examples, 1 failure Failed examples: rspec ./spec/models/relationship_spec.rb:7 # Relationship#create can save user_idとfollow_idがあれば登録できる

試したこと

上記エラーでgot errors: Follower must exist, Followed must existと出たので、
試しにfactorybotを

ruby

1FactoryBot.define do 2 3 factory :relationship do 4 follower {1} 5 followed {2} 6 end 7 8end

としたところ、再度エラー

1) Relationship#create can save user_idとfollow_idがあれば登録できる Failure/Error: relationship = build(:relationship) ActiveRecord::AssociationTypeMismatch: User(#70152444865000) expected, got 1 which is an instance of Integer(#70152448162980) # ./spec/models/relationship_spec.rb:8:in `block (4 levels) in <top (required)>' Finished in 1.6 seconds (files took 7.32 seconds to load) 1 examples, 1 failure Failed examples: rspec ./spec/models/relationship_spec.rb:7 # Relationship#create can save user_idとfollow_idがあれば登録できる

上記エラーの理由が見当つかず、困っています。
解決方法がわかる方、ご指導いただけますと幸いです。
宜しくお願い致します。

補足情報(FW/ツールのバージョンなど)

Ruby 2.5.1
Rails 5.2.3

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問