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

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

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

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

RSpec

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

Ruby on Rails

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

Q&A

解決済

2回答

5847閲覧

【RSpec】destroyアクションのテストが通らない

kumamin

総合スコア12

Ruby

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

RSpec

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/10/02 02:51

編集2020/10/03 00:03

前提・実現したいこと

今、Railsで開発したアプリのテストをRSpecで行っています。

そのアプリには通知機能があり、通知を削除するテストが通らないので、
解決策を教えていただけますと幸いです。

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

ruby

1NotificationsController 2 DELETE #destroy 3 ユーザーがログインしている場合 4 通知を削除できること (FAILED - 1) 5 6Failures: 7 8 1) NotificationsController DELETE #destroy ユーザーがログインしている場合 通知を削除できること 9 Failure/Error: expect{ subject }.to change(Notification, :count).by(-1) 10 expected `Notification.count` to have changed by -1, but was changed by 0 11 # ./spec/controllers/notifications_controller_spec.rb:52:in `block (4 levels) in <top (required)>' 12 13Finished in 0.24758 seconds (files took 1.48 seconds to load) 141 example, 1 failure 15 16Failed examples: 17 18rspec ./spec/controllers/notifications_controller_spec.rb:50 # NotificationsController DELETE #destroy ユーザーがログインしている場合 通知を削除できること 19

該当のソースコード

▼notifications_controller_spec.rb

ruby

1require 'rails_helper' 2 3describe NotificationsController do 4 let(:user) { create(:user) } 5 let(:notification) { create(:notification) } 6 7 describe 'DELETE #destroy' do 8 subject { 9 delete :destroy, 10 params: {id: notification.id} 11 } 12 13 context "ユーザーがログインしている場合" do 14 before do 15 login user 16 end 17 18 it "通知を削除できること" do 19 expect{ subject }.to change(Notification, :count).by(-1) 20 end 21 end 22 end 23 24end

▼spec/factories/notifications.rb

ruby

1FactoryBot.define do 2 factory :notification do 3 association :visitor, factory: :user 4 association :visited, factory: :user 5 checked { true } 6 action { "message" } 7 end 8end

▼app/models/notification.rb

ruby

1class Notification < ApplicationRecord 2 default_scope->{order(created_at: :desc)} 3 4 belongs_to :group, optional: true 5 belongs_to :visitor, class_name: 'User', foreign_key: 'visitor_id' 6 belongs_to :visited, class_name: 'User', foreign_key: 'visited_id' 7 ACTION_VALUES = ["message", "follow"] 8 validates :action, presence: true, inclusion: {in: ACTION_VALUES} 9 validates :checked, presence: true, inclusion: {in: [true, false]} 10end

▼notifications_controller.rb

ruby

1class NotificationsController < ApplicationController 2 before_action :authenticate_user! 3 4 def destroy 5 notification = Notification.find(params[:id]) 6 notification.destroy 7 redirect_to notifications_path 8 end 9 10end

試したこと

test.logのファイルを確認したのですが、間違いがわかりませんでした。こちらがログの画像です。
イメージ説明

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

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

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

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

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

guest

回答2

0

自己解決

notifications_controller_spec.rbで以下のように修正したら解決しました。
変更前:let(:notification) { create(:notification) }
変更後:let!(:notification) { create(:notification) }(letの後に!を追加)

投稿2020/10/03 05:37

kumamin

総合スコア12

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

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

0

subject
expect{ subject }.to change(Notification, :count).by(-1)
としていますが,これだと2回削除リクエストを出してることになると思うので,
expect{ subject }.to change(Notification, :count).by(-1)
だけで,動くのではないでしょうか?

投稿2020/10/02 03:39

kmdkuk

総合スコア141

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

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

kumamin

2020/10/02 04:26

ご回答ありがとうございます。 確かにダブってしまっていますね。 ただ、1回目のsubjectを削除してみたのですが、エラー文は変わりませんでした。
kmdkuk

2020/10/02 04:40

なるほど,,, なんでだろう. ログ出力も特に変わらなかった感じですかね?
kumamin

2020/10/03 00:00

返信が遅れてしまい、申し訳ございません。 ログも変わっていないと思います。 一応質問に載せたログの画像を更新しておきます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問