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

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

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

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

RSpec

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

Ruby on Rails

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

Q&A

解決済

1回答

1122閲覧

rspecでのエラーを解決したい

k-810

総合スコア192

Ruby

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

RSpec

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

Ruby on Rails

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

0グッド

0クリップ

投稿2019/09/05 17:34

編集2019/09/05 17:58

rspecでのエラーを解決したい

controllerのテストで発生した下記のエラーを解決したい

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

1) Api::V1::CommentsController#create データベースに新しいコメントが登録されること Failure/Error: expect{ subject }.to change(Comment, :count).by(1) expected `Comment.count` to have changed by 1, but was changed by 0 # ./spec/controllers/api/v1/comments_controller_spec.rb:41:in `block (3 levels) in <main>'

comments_controller_spec.rb

describe "#create" do let(:params) { { comment: build(:comment).attributes } } subject { post :create, params: params } it "データベースに新しいコメントが登録されること" do expect{ subject }.to change(Comment, :count).by(1) end end

comments_controller.rb

def create @comment = Comment.create(comment_params) end private def comment_params params.require(:comment).permit(:profile_id, :post_id, :text) end end

spec/factories/comments.rb

FactoryBot.define do factory :comment do profile post{ create(:post, profile: profile) } text "hogehoge" end end

###アソシエーション

class Comment < ApplicationRecord belongs_to :post belongs_to :profile end class Post < ApplicationRecord belongs_to :profile end class Profile < ApplicationRecord has_many :posts, dependent: :destroy has_many :comments, dependent: :destroy end

ちなみにcreate(:comment)自体は通ります

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

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

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

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

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

guest

回答1

0

ベストアンサー

p メソッドなどで各種変数に関してデバッグしてみてください

def create p params # params は想定通りか p comment_params # 正しく permit されているか @comment = Comment.create(comment_params) p @comment # commentは作られているか p @comment.errors # エラーは起きていないか end

追記

factory_bot/issues/1255

????に書いてあるとおり、

ruby

1FactoryBot.define do 2 factory :comment do 3 profile #<= buildしたときに生成されない 4 post{ create(:post, profile: profile) } 5 text "hogehoge" 6 end 7end

ようなので、上記Issueにあるように FactoryBot.use_parent_strategy = false を設定するか

Ruby

1FactoryBot.define do 2 factory :comment do 3 profile { create(:profile) } 4 post{ create(:post, profile: profile) } 5 text "hogehoge" 6 end 7end

のようにする必要がありそうです。

投稿2019/09/06 00:14

編集2019/09/06 05:46
unhappychoice

総合スコア1531

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

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

k-810

2019/09/06 05:34

回答ありがとうございます。 paramsが上手くいっていないようでした。 comments_controller_spec.rbの let(:params) { { comment: build(:comment).attributes } } の部分が間違っているのでしょうか。解決方法が見つかりません。 以下,エラー文です。 ``` #create <ActionController::Parameters {"comment"=>{"created_at"=>"", "id"=>"", "post_id"=>"598", "profile_id"=>"", "text"=>"hogehoge", "updated_at"=>""}, "controller"=>"api/v1/comments", "action"=>"create"} permitted: false> <ActionController::Parameters {"profile_id"=>"", "post_id"=>"598", "text"=>"hogehoge"} permitted: true> #<Comment id: nil, post_id: 598, text: "hogehoge", created_at: nil, updated_at: nil, profile_id: nil> #<ActiveModel::Errors:0x000055abc2c2f308 @base=#<Comment id: nil, post_id: 598, text: "hogehoge", created_at: nil, updated_at: nil, profile_id: nil>, @messages={:profile=>["を入力してください"]}, @details={:profile=>[{:error=>:blank}]}> データベースに新しいコメントが登録されること (FAILED - 2) ```
k-810

2019/09/06 06:43

解決しました!ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問