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

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

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

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

RSpec

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

Q&A

0回答

506閲覧

rspecでテストが通らない

ik_ko

総合スコア9

Ruby on Rails 6

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

RSpec

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

0グッド

0クリップ

投稿2021/04/08 11:27

テストを書いていたのですがその中でエラーで詰まってしまったでご教授お願いします。
#状況
comments_spec.rbのテストを実行じにエラーが発生し、以下のエラー分が出ました。

Failures: 1) Api::Comments GET /api/comments 200 status Failure/Error: expect(response).to have_http_status(200) expected the response to have status code 200 but it was 500 # ./spec/requests/api/comments_spec.rb:10:in `block (3 levels) in <top (required)>' Finished in 1.33 seconds (files took 7.24 seconds to load) 1 example, 1 failure Failed examples: rspec ./spec/requests/api/comments_spec.rb:8 # Api::Comments GET /api/comments 200 status ERROR: 1

#各種ファイル

spec/requests/api/comments_spec.rb

require 'rails_helper' RSpec.describe 'Api::Comments', type: :request do let!(:user) {create(:user)} let!(:article) {create(:article, user: user)} let!(:comments) {create_list(:comment, 3, user: user, article: article)} describe 'GET /api/comments' do it '200 status' do get api_comments_path(article_id: article.id) expect(response).to have_http_status(200) end end end

factories/comments.rb

FactoryBot.define do factory :comment do content { Faker::Lorem.characters(number: 30) } end end

factories/users.rb

FactoryBot.define do factory :user do email { Faker::Internet.email } password { 'password' } account {'ikikikik'} trait :with_profile do after :build do |user| build(:profile, user: user) end end end end

factories/articles.rb

FactoryBot.define do factory :article do object { '物品' } price { 3000 } store { '店' } content { 'ありがとう'} rate { 1 } association :user association :category end end

api/comments_controller.rb

class Api::CommentsController < Api::ApplicationController before_action :set_article, only: [:index, :create] def index comments = @article.comments render json: comments end def create comment = @article.comments.build(comment_params) comment.save! #通知作成 @article.create_notification_comment!(current_user, comment.id) render json: comment end private def set_article @article = Article.find(params[:article_id]) end def comment_params params.require(:comment).permit(:content).merge(user_id: current_user.id) end end

routes.rb

namespace :api, defaluts: [format: :json] do scope '/articles/:article_id' do resources :comments, only: [:index, :create, :destroy] resource :like, only: [:show, :create, :destroy] end scope '/accounts/:account_id' do resources :informations, only: [:index] end end

エラー文でも検索してみましたが原因がわかりませんでした。
ステータスコードが500だったのでサーバー側の問題なのはわかるのですが、コントローラーの記載も間違っていないように思えるのですみませんがわかる方いましたらご教授お願いします。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問