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

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

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

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

Ruby on Rails 6

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

Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

Ruby on Rails

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

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

1回答

2568閲覧

rspec mysql ForeignKey errorの解決

kei__3

総合スコア7

Ruby

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

Ruby on Rails 6

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

Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

Ruby on Rails

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

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2020/11/01 03:36

編集2020/11/06 00:43

前提・実現したいこと

現在、作成したオリジナルアプリをhttps://qiita.com/naoki_mochizuki/items/5a1757d222806cbe0cd1こちらの記事に従ってawsにデプロイしようとしています。こちらの記事はMysqlを前提としていたので、https://note.com/itoa06/n/n31fe4f9cd6b9こちらを参考に変更しました。
変更後、rcpecを行うとすべて通っていたはずが通らなくなりました。
解決方法わかる方いましたら教えて頂きたいです。
また、下記のエラーはrspecとし、全体をテストした時に起こります。 rspec spec/requests等とするとエラーが起きません。なぜでしょうか?

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

Failures: 1) User is posts from unfollowed users are not displayed Failure/Error: FactoryBot.create(:post, :post_image, user: other_user) ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/models/user_spec.rb:94:in `block (2 levels) in <main>' # ------------------ # --- Caused by: --- # Mysql2::Error: # Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/models/user_spec.rb:94:in `block (2 levels) in <main>' 2) Comments #create as an authenticated user responds successfully Failure/Error: let(:post_image) { FactoryBot.create(:post, :post_image, user: user) } ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/comments_request_spec.rb:7:in `block (2 levels) in <main>' # ./spec/requests/comments_request_spec.rb:8:in `block (2 levels) in <main>' # ------------------ # --- Caused by: --- # Mysql2::Error: # Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/comments_request_spec.rb:7:in `block (2 levels) in <main>' 3) Comments #create as an guest user is can't comment on a post Failure/Error: let(:post_image) { FactoryBot.create(:post, :post_image, user: user) } ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/comments_request_spec.rb:7:in `block (2 levels) in <main>' # ./spec/requests/comments_request_spec.rb:8:in `block (2 levels) in <main>' # ------------------ # --- Caused by: --- # Mysql2::Error: # Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/comments_request_spec.rb:7:in `block (2 levels) in <main>' 4) Comments #destroy as other user is can't delete comment Failure/Error: let(:post_image) { FactoryBot.create(:post, :post_image, user: user) } ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/comments_request_spec.rb:7:in `block (2 levels) in <main>' # ./spec/requests/comments_request_spec.rb:8:in `block (2 levels) in <main>' # ------------------ # --- Caused by: --- # Mysql2::Error: # Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/comments_request_spec.rb:7:in `block (2 levels) in <main>' 5) Like #create as an authenticated user responds successfully Failure/Error: let!(:post_image) { FactoryBot.create(:post, :post_image, user: user) } ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/like_request_spec.rb:6:in `block (2 levels) in <main>' # ------------------ # --- Caused by: --- # Mysql2::Error: # Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/like_request_spec.rb:6:in `block (2 levels) in <main>' 6) Like #create as an authenticated user is can like posts Failure/Error: let!(:post_image) { FactoryBot.create(:post, :post_image, user: user) } ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/like_request_spec.rb:6:in `block (2 levels) in <main>' # ------------------ # --- Caused by: --- # Mysql2::Error: # Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) ...まだ続きます

spec/models/user_spec.rb

require 'rails_helper' RSpec.describe User, type: :model do let(:user) { FactoryBot.create(:user) } let(:other_user) { FactoryBot.create(:user, email: "other_user@example.com") } . . . # フォローしていないユーザーの投稿は表示されない it "is posts from unfollowed users are not displayed" do FactoryBot.create(:other_user) FactoryBot.create(:post, :post_image, user: other_user) other_user.posts.each do |unfollowed| expect(user.feed).to_not include(unfollowed) end end end

###app/models/user.rb

class User < ApplicationRecord has_one_attached :icon has_many :likes, dependent: :destroy has_many :liked_posts, through: :likes, source: :user has_many :posts, dependent: :destroy has_many :comments, dependent: :destroy has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy has_many :passive_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy has_many :following, through: :active_relationships, source: :followed has_many :followers, through: :passive_relationships attr_accessor :remember_token, :activation_token, :reset_token before_save :downcase_email before_create :create_activation_digest validates :name, presence: true, length: { maximum:15 } 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: true has_secure_password validates :password, presence: true, length: { minimum: 6 }, allow_nil: true ...

###app/models/post.rb

class Post < ApplicationRecord belongs_to :user has_many :likes, dependent: :destroy has_many :liked_users, through: :likes, source: :user has_many :comments, dependent: :destroy has_one_attached :image default_scope -> { order(created_at: :desc) } validates :user_id, presence: true validates :name, presence: true, length: { maximum:50 } validates :content, presence: true, length: { maximum:140 } validates :image, presence:true, content_type: { in: %w[image/jpeg image/gif image/png], message: "有効な画像形式である必要があります" }, size: { less_than: 5.megabytes, message: "5MB未満である必要があります" } def display_image image.variant(resize_to_fill:[280,600]) end def like_by?(user) likes.where(user_id: user_id).exist? end def self.sort_like Post.all.sort{|a,b| b.liked_users.count <=> a.liked_users.count} end end

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

Rails 6.0.3.2
ruby 2.7.1
ubuntu 18.04 LTS
ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

ベストアンサー

modelのcodeもtable のschemaもない、しかもえらーの起きたspecが載って居ない。。。。のでエスパーモードです。
Failure/Error: FactoryBot.create(:post, :post_image, user: other_user)
ここで
ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails ('viewhome_test'.'posts', CONSTRAINT 'fk_rails_5b5ddfd518' FOREIGN KEY ('user_id'

が起きているということは、actoryBot.create(:post, する前に other_user が作られていないためでしょう

投稿2020/11/01 04:04

winterboum

総合スコア23284

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

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

kei__3

2020/11/01 05:08

申し訳ございません。。。 編集で追加しましたが、文字数制限に引っかかってしまうため一部省略しています。 table のschemaとは下記のものでよろしいでしょうか。 ``` mysql> use viewhome_test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show COLUMNS FROM `users` ; +-------------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------------+--------------+------+-----+---------+----------------+ | id | bigint(20) | NO | PRI | NULL | auto_increment | | name | varchar(255) | YES | | NULL | | | email | varchar(255) | YES | UNI | NULL | | | created_at | datetime(6) | NO | | NULL | | | updated_at | datetime(6) | NO | | NULL | | | password_digest | varchar(255) | YES | | NULL | | | remember_digest | varchar(255) | YES | | NULL | | | admin | tinyint(1) | YES | | 0 | | | activation_digest | varchar(255) | YES | | NULL | | | activated | tinyint(1) | YES | | 0 | | | activated_at | datetime | YES | | NULL | | | reset_digest | varchar(255) | YES | | NULL | | | reset_sent_at | datetime | YES | | NULL | | +-------------------+--------------+------+-----+---------+----------------+ ``` FactoryBot.create(:post...の前に FactoryBot.create(:other_user)を追加すると下記のエラーがでるようになりました。 Failure/Error: FactoryBot.create(:other_user) KeyError: Factory not registered: "other_user" # ./spec/models/user_spec.rb:94:in `block (2 levels) in <main>' # ------------------ # --- Caused by: --- # KeyError: # key not found: "other_user" # ./spec/models/user_spec.rb:94:in `block (2 levels) in <main>' letで使えるようにしていると思うのですが、、、
kei__3

2020/11/01 05:25

rspecを行うと、上記のようにエラーがいくつもでたり、ほとんど通ったりします。なにが原因でしょうか?? Failures: 1) User is posts from unfollowed users are not displayed Failure/Error: FactoryBot.create(:other_user) KeyError: Factory not registered: "other_user" # ./spec/models/user_spec.rb:94:in `block (2 levels) in <main>' # ------------------ # --- Caused by: --- # KeyError: # key not found: "other_user" # ./spec/models/user_spec.rb:94:in `block (2 levels) in <main>' 2) Relationships #delete as an authenticated user is can following user Failure/Error: @user = Relationship.find(params[:id]).followed ActiveRecord::RecordNotFound: Couldn't find Relationship with 'id'=948 # ./app/controllers/relationships_controller.rb:14:in `destroy' # ./spec/requests/relationships_request_spec.rb:49:in `block (5 levels) in <main>' # ./spec/requests/relationships_request_spec.rb:48:in `block (4 levels) in <main>' Finished in 22.17 seconds (files took 8.87 seconds to load) 84 examples, 2 failures
winterboum

2020/11/01 06:15

table のschemaとはそれでもまぁ間違いでな無いですが、Railsの世界では db/schema.rb か db/migration をのせることが多いです。そのほうが情報が密なので
winterboum

2020/11/01 06:17

「rspecを行うと、上記のようにエラーがいくつもでたり、ほとんど通ったりします。」 test を順番にやるモードとランダムにやるモードがあります。 other_userができていたりできていなかったりするのでしょう
kei__3

2020/11/01 09:01

そうだったのですね。。 ありがとうございます。 モードがあるなんて知りませんでした。今までは順番に依存していたテストを書いていて依存したテストを行い通っていたということなのでしょうか?
kei__3

2020/11/01 09:05

# This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # This file is the source Rails uses to define your schema when running `rails # db:schema:load`. When creating a new database, `rails db:schema:load` tends to # be faster and is potentially less error prone than running all of your # migrations from scratch. Old migrations may fail to apply correctly if those # migrations use external dependencies or application code. # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2020_10_23_034803) do create_table "active_storage_attachments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false t.bigint "record_id", null: false t.bigint "blob_id", null: false t.datetime "created_at", null: false t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id" t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true end create_table "active_storage_blobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "key", null: false t.string "filename", null: false t.string "content_type" t.text "metadata" t.bigint "byte_size", null: false t.string "checksum", null: false t.datetime "created_at", null: false t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true end create_table "comments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.integer "user_id", null: false t.integer "post_id", null: false t.string "text", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end create_table "likes", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.integer "user_id" t.integer "post_id" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["post_id"], name: "index_likes_on_post_id" t.index ["user_id", "post_id"], name: "index_likes_on_user_id_and_post_id", unique: true t.index ["user_id"], name: "index_likes_on_user_id" end create_table "posts", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name" t.text "content" t.bigint "user_id", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["user_id", "created_at"], name: "index_posts_on_user_id_and_created_at" t.index ["user_id"], name: "index_posts_on_user_id" end create_table "relationships", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.integer "follower_id" t.integer "followed_id" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["followed_id"], name: "index_relationships_on_followed_id" t.index ["follower_id", "followed_id"], name: "index_relationships_on_follower_id_and_followed_id", unique: true t.index ["follower_id"], name: "index_relationships_on_follower_id" end create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name" t.string "email" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.string "password_digest" t.string "remember_digest" t.boolean "admin", default: false t.string "activation_digest" t.boolean "activated", default: false t.datetime "activated_at" t.string "reset_digest" t.datetime "reset_sent_at" t.index ["email"], name: "index_users_on_email", unique: true end add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "posts", "users" end
winterboum

2020/11/01 11:57

add_foreign_key "posts", "users" がありますね。 これにより、user_id に値が有るだけでなく、それが実在するUserのものである、という条件が付きます。
winterboum

2020/11/01 11:58

「今までは順番に依存していたテストを書いていて依存したテストを行い通っていたということなのでしょうか?」 多分そうだろうと思います。
kei__3

2020/11/02 15:19

わかりました。いつもありがとうございます。 再度テストを書き直してみようと思います。
kei__3

2020/11/04 15:32

実在するUserとは、let(:user) { FactoryBot.create(:user) }で作っているuserでもダメなのでしょうか。また、ランダムで実行する際このletもランダムに変化するのでしょうか。
winterboum

2020/11/04 23:22

let は userを参照する都度実行されますので、ランダムに実行する都度実行され常に新しいuserを作ります。
kei__3

2020/11/05 00:52

新しいuserを作るということは、実在するuserではないということでしょうか??また、実在するuserでない場合、RSpecでどのようにすれば実在するuserを作れるもしくは取り出すことができるでしょうか?
winterboum

2020/11/05 01:04

Botの作り方によるのですが、uniqueになるべき項目が複数回letすることでuniqueにならなくなる時があります。そのときのテストはuserがnilとなってしまいます。
kei__3

2020/11/05 07:48

Botのemailを同じにしていたので、sequenceを用いて、uniqueにしたのですが、ForeignKey errorが何個もでてきます。 「複数回letすることでuniqueにならなくなる時があります」 そのような場合はどのような事をすれば解消されるでしょうか。 何度も申し訳ございません。
winterboum

2020/11/05 08:18

sequence でなく、Fakerをつかってみてください
kei__3

2020/11/05 11:23

Fakerを使用したのですが、変わらず、 ForeignKey errorがでてしまいます。 =====factories/user.rb====== FactoryBot.define do factory :user do name { "テストユーザー" } email { Faker::Internet.email } password { "password" } password_confirmation { "password" } activated { true } end factory :other_user, class: User do name { "otheruser" } email { "other_user@example.com" } password { "password" } password_confirmation { "password" } activated { true } end end =====factories/posts.rb===== FactoryBot.define do factory :post do name { 'テスト' } content { 'テスト' } trait :post_image do image { fixture_file_upload("app/assets/images/IMG_7226.PNG") } end end end =====requests/posts_request_spec.rb====== require 'rails_helper' RSpec.describe "Posts", type: :request do let(:user) { FactoryBot.create(:user) } let(:other_user) { FactoryBot.create(:other_user) } let(:guest_user) { FactoryBot.create(:user, email: "guest@example.com") } let(:post_image) { FactoryBot.create(:post,:post_image, user: user) } describe "#create" do # ログインしているユーザー context "as an authenticated user" do # 正常なレスポンスを返すこと it "responds successfully" do sign_in_as user post posts_path, params: { post:{ post_image: post_image} } expect(response).to be_successful expect(response).to have_http_status "200" end end # ゲストユーザーの場合 context "as an guest user" do # 投稿できないこと it "is can't post" do sign_in_as guest_user expect{ post posts_path, params: { post: post_image,user: guest_user } }.to_not change(Post, :count) end end # ログインしていないユーザー context "as a not authenticated user" do #ログイン画面にリダイレクトすること it "is redirects to the login page" do post posts_path,params: { post: post_image } expect(response).to redirect_to login_path end end end describe "#destroy" do # ログインしていないユーザー context "as a not authenticated user" do # ログイン画面にリダイレクトすること it "is redirects to the login page" do expect { delete post_path(post_image) }.to_not change(Post, :count) expect(response).to redirect_to login_path end end # アカウントが違うユーザー context "as other user" do # ホーム画面にリダイレクトすること it "is redirects to the root page" do sign_in_as other_user expect { delete post_path(post_image),params: {id: post_image.id} }.to_not change(Post, :count) expect(response).to redirect_to root_path end end end end =====でているエラー===== Posts #create as an authenticated user responds successfully Failure/Error: let(:post_image) { FactoryBot.create(:post,:post_image, user: user) } ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/posts_request_spec.rb:7:in `block (2 levels) in <main>' # ./spec/requests/posts_request_spec.rb:16:in `block (4 levels) in <main>' # ------------------ # --- Caused by: --- # Mysql2::Error: # Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/posts_request_spec.rb:7:in `block (2 levels) in <main>' 9) Posts #create as an guest user is can't post Failure/Error: let(:post_image) { FactoryBot.create(:post,:post_image, user: user) } ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/posts_request_spec.rb:7:in `block (2 levels) in <main>' # ./spec/requests/posts_request_spec.rb:28:in `block (5 levels) in <main>' # ./spec/requests/posts_request_spec.rb:27:in `block (4 levels) in <main>' # ------------------ # --- Caused by: --- # Mysql2::Error: # Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/posts_request_spec.rb:7:in `block (2 levels) in <main>' 10) Posts #create as a not authenticated user is redirects to the login page Failure/Error: let(:post_image) { FactoryBot.create(:post,:post_image, user: user) } ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/posts_request_spec.rb:7:in `block (2 levels) in <main>' # ./spec/requests/posts_request_spec.rb:37:in `block (4 levels) in <main>' # ------------------ # --- Caused by: --- # Mysql2::Error: # Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/posts_request_spec.rb:7:in `block (2 levels) in <main>' 11) Posts #destroy as a not authenticated user is redirects to the login page Failure/Error: let(:post_image) { FactoryBot.create(:post,:post_image, user: user) } ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/posts_request_spec.rb:7:in `block (2 levels) in <main>' # ./spec/requests/posts_request_spec.rb:49:in `block (5 levels) in <main>' # ./spec/requests/posts_request_spec.rb:48:in `block (4 levels) in <main>' # ------------------ # --- Caused by: --- # Mysql2::Error: # Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/posts_request_spec.rb:7:in `block (2 levels) in <main>' 12) Posts #destroy as other user is redirects to the root page Failure/Error: let(:post_image) { FactoryBot.create(:post,:post_image, user: user) } ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/posts_request_spec.rb:7:in `block (2 levels) in <main>' # ./spec/requests/posts_request_spec.rb:61:in `block (5 levels) in <main>' # ./spec/requests/posts_request_spec.rb:60:in `block (4 levels) in <main>' # ------------------ # --- Caused by: --- # Mysql2::Error: # Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/posts_request_spec.rb:7:in `block (2 levels) in <main>' なにが原因でしょうか。。。。
winterboum

2020/11/05 12:43

guest_user が Fakerになってないですね
kei__3

2020/11/05 13:11

=====factories/users.rb===== FactoryBot.define do factory :user do name { "テストユーザー" } email { Faker::Internet.email } password { "password" } password_confirmation { "password" } activated { true } end factory :other_user, class: User do name { "otheruser" } email { Faker::Internet.email } password { "password" } password_confirmation { "password" } activated { true } end factory :guest_user, class: User do name { "guestuser" } email { Faker::Internet.email } password { "password" } password_confirmation { "password" } activated { true } end end =====posts_request_spec.rb====== require 'rails_helper' RSpec.describe "Posts", type: :request do let(:user) { FactoryBot.create(:user) } let(:other_user) { FactoryBot.create(:other_user) } let(:guest_user) { FactoryBot.create(:guest_user) } let(:post_image) { FactoryBot.create(:post,:post_image, user: user) } describe "#create" do # ログインしているユーザー context "as an authenticated user" do # 正常なレスポンスを返すこと it "responds successfully" do sign_in_as user post posts_path, params: { post:{ post_image: post_image} } expect(response).to be_successful expect(response).to have_http_status "200" end end # ゲストユーザーの場合 context "as an guest user" do # 投稿できないこと it "is can't post" do sign_in_as guest_user expect{ post posts_path, params: { post: post_image,user: guest_user } }.to_not change(Post, :count) end end # ログインしていないユーザー context "as a not authenticated user" do #ログイン画面にリダイレクトすること it "is redirects to the login page" do post posts_path,params: { post: post_image } expect(response).to redirect_to login_path end end end describe "#destroy" do # ログインしていないユーザー context "as a not authenticated user" do # ログイン画面にリダイレクトすること it "is redirects to the login page" do expect { delete post_path(post_image) }.to_not change(Post, :count) expect(response).to redirect_to login_path end end # アカウントが違うユーザー context "as other user" do # ホーム画面にリダイレクトすること it "is redirects to the root page" do sign_in_as other_user expect { delete post_path(post_image),params: {id: post_image.id} }.to_not change(Post, :count) expect(response).to redirect_to root_path end end end end に変えても変化がない状態です。。。
winterboum

2020/11/05 22:57

はて、何を見落としているんだろう。。。 念の為 モデル Post の関連定義質問欄の方に載せてください。 それと、エラーはPostが作れないことなので、models/post_spec.rb で試してください。 よく読むとlet(:post_image) { FactoryBot.create(:post,:post_image, user: user) }とあります。createの中の:post_image ですがこういう使い方は初めてみるのですが、合ってます?
kei__3

2020/11/06 00:49

models/post_spec.rbにある # アプリ名とオススメポイントとホーム画像があれば有効である it "is valid with a name, content, and homeimages " do post = FactoryBot.build(:post, :post_image, user: user) expect(post).to be_valid end は成功します。 postは presence: trueのバリテーションをつけています。 ですので、spec/factories/posts.rbで FactoryBot.define do factory :post do name { 'テスト' } content { 'テスト' } trait :post_image do image { fixture_file_upload("app/assets/images/IMG_7226.PNG") } end end end このように定義して、その画像を使えるようにしているつもりです。
winterboum

2020/11/06 01:13

trait :post_image 見落としてました models/post_spec.rb は通ってるか。。。 するとspec/controllersの問題ですね post posts_path, params: { post:{ post_image: post_image} } が怪しいか。 ああ、 POST のrequestのパラメーターの形になっていないですね。 ここは post: {name: ”名前”,comment: "content", user_id: user.id} の形式が必要です。ここでuser_idが指定されていないのが原因でしょう。
kei__3

2020/11/06 02:30

ありがとうございます。 post posts_path, params: {post: { name: "テスト", content: "テスト", user_id: user.id } }とすると、ForeignKey errorが消えました。(別のエラーになりました) postのrequestパラメーターの形式というのはどのように判断すれば良いのでしょうか。他のrequestspecに役立てたいです。 また、deleteの時の形式の判断方法も知りたいです。
winterboum

2020/11/06 02:34

ブラウザーでpostして、そのlogを見ればわかります。 別のエラーは「validates :image, presence:true」からみですね。
kei__3

2020/11/06 05:36

別のエラーは 9) Posts #create as an authenticated user responds successfully Failure/Error: expect(response).to be_successful expected `#<ActionDispatch::TestResponse:0x00007ffff1c4d9e0 @mon_data=#<Monitor:0x00007ffff1c4d990>, @mon_data_..., @method=nil, @request_method=nil, @remote_ip=nil, @original_fullpath=nil, @fullpath=nil, @ip=nil>>.successful?` to be truthy, got false # ./spec/requests/posts_request_spec.rb:17:in `block (4 levels) in <main>' 10) Posts #create as an guest user is can't post Failure/Error: expect{ post posts_path, params: {post: { name: "テスト", content: "テスト", user_id: user.id } } }.to_not change(Post, :count) expected `Post.count` not to have changed, but did change from 2028 to 0 等です。 Started DELETE "/posts/8" for ::1 at 2020-11-06 14:30:16 +0900 (0.5ms) SET NAMES utf8, @@SESSION.sql_mode = 'TRADITIONAL', @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483 Processing by PostsController#destroy as HTML Parameters: {"authenticity_token"=>"B4wFcFPTQsCTua87VMPe+PM1uq+El+wZ1ZtlTnM6vW0ckMKhZ8dWG5+UkCOC0FKTksyiejUCRvGmnXWk964kSQ==", "id"=>"8"} ブラウザでdeleteした際のlogなのですが、 ForeignKey errorがでてしまいます。 describe "#destroy" do # ログインしていないユーザー context "as a not authenticated user" do # ログイン画面にリダイレクトすること it "is redirects to the login page" do expect { delete post_path(post_image),params: {id: post_image.id,user_id: user.id} }.to_not change(Post, :count) expect(response).to redirect_to login_path end end 形が間違っているでしょうか。
winterboum

2020/11/06 06:02

どの foreign keyか言ってない?
kei__3

2020/11/06 06:11

user_idだと思います。 11) Posts #destroy as a not authenticated user is redirects to the login page Failure/Error: let(:post_image) { FactoryBot.create(:post,:post_image, user: user) } ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/posts_request_spec.rb:7:in `block (2 levels) in <main>' # ./spec/requests/posts_request_spec.rb:49:in `block (5 levels) in <main>' # ./spec/requests/posts_request_spec.rb:48:in `block (4 levels) in <main>' # ------------------ # --- Caused by: --- # Mysql2::Error: # Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/posts_request_spec.rb:7:in `block (2 levels) in <main>'
winterboum

2020/11/06 06:18

話が矛盾してるんで疲れます。 「ブラウザでdeleteした際のlogなのですが、ForeignKey errorがでてしまいます。」 に対して「どの foreign keyか言ってない?」と聞いた答えが なぜ、rspecの情報が出てくるの?
kei__3

2020/11/06 06:26

申し訳ございません。 上記がブラウザでdeleteした際のlog そのlogを見てspecを書いたのですが、エラーがでてしまうという意味でした。 ですので、rspecの事だと思いました。失礼致しました。
winterboum

2020/11/06 06:28

「上記がブラウザでdeleteした際のlog」にはエラーが出てませんが。
kei__3

2020/11/06 06:40

でていないです。私の言い方が悪かったです。申し訳ありません。
winterboum

2020/11/06 09:08

deleteのrspecでforeign keyのエラーが出ているのは、削除するpostをcreateしている時ですから、そこ確認してみて。
kei__3

2020/11/07 01:45

ありがとうございます。 let(:post_image) { FactoryBot.create(:post,:post_image, user: user,user_id:user.id) }にしたり、userを作っておくために、let!(:user) { FactoryBot.create(:user) }、postのfactoriesに関連付けるために、 factories/posts.rbを FactoryBot.define do factory :post do user name { 'テスト' } content { 'テスト' } trait :post_image do image { fixture_file_upload("app/assets/images/IMG_7226.PNG") } end end end 等にしましたが変わらない状態です。。。 また、テストの順序を固定して解決していこうと思い、rspec --seed 46598 としているのですが、この場合でも、foreign keyエラーがでたり、でなかったりします。。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問