0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

9.22 振り返り RSpecいいねのエラー>>遅延評価 「ruby標準出力(まだよくわからない)」

Last updated at Posted at 2021-09-22
tail -f log/test.log 
tail で後方から指定した行数分表示できる  ログが見れる

favorite.spec.rb
RSpec.describe "Staff::Favorites", type: :request do
  let(:staff){FactoryBot.create(:staff)}
  let(:ostomy){FactoryBot.create(:ostomy)}
  let(:patient){FactoryBot.create(:patient)}
  let(:favorite){FactoryBot.create(:favorite, staff: staff, ostomy: ostomy)}

  context 'ログインしている場合' do
   before do
     @staff = FactoryBot.create(:staff)
     sign_in staff
   end

   let(:ostomy_params)do
    {
    patient_id: patient.id,
    color: 'pink',
    edema: 'normal',
    skin: 'same',
    h_size: '30 ',
    w_size: '30',
    comment: 'ok',
    #image: '',
    }
    end

    describe 'PATCH /staff/ostomies/:ostomy_id/favorites' do
      it 'いいねを押す' do
        patch staff_ostomy_favorites_path(ostomy, params:{ ostomy:ostomy_params})
        # (ostomy, params:{ ostomy:ostomy_params})
        favorite.reload
      end
    end

     describe 'DELETE /staff/ostomies/:ostomy_id/favorites' do
      it 'いいね取り消す' do
        expect{
          delete staff_ostomy_favorites_path(ostomy)
         }.to change { Favorite.all.count }.by(-1)
      end
     end

    end
  end

Staff::Favorites
  ログインしている場合
    DELETE /staff/ostomies/:ostomy_id/favorites
      いいね取り消す (FAILED - 1)

Failures:

  1) Staff::Favorites ログインしている場合 DELETE /staff/ostomies/:ostomy_id/favorites いいね取り消す
     Failure/Error:
       expect{
         delete staff_ostomy_favorites_path(ostomy)
        }.to change { Favorite.all.count }.by(-1)
     
       expected `Favorite.all.count` to have changed by -1, but was changed by 0
     # ./spec/requests/staff/favorite_spec.rb:40:in `block (4 levels) in <top (required)>'

Finished in 0.37761 seconds (files took 2.17 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/requests/staff/favorite_spec.rb:39 # Staff::Favorites ログインしている場合 DELETE /staff/ostomies/:ostomy_id/favorites いいね取り消す

なにでエラーが出てるかわからない

p '-------------' p Favorite.all.count

p Favorite.all.count
p '-------------'
を入れて出力させ、状態変化を見た

favorite.spec.rb
describe 'DELETE /staff/ostomies/:ostomy_id/favorites' do
      it 'いいね取り消す' do
    p '-------------'
    p Favorite.all.count
        expect{
          delete staff_ostomy_favorites_path(ostomy)

         }.to change { Favorite.all.count }.by(-1)
       
    p Favorite.all.count
    p '-------------'
      end
ターミナル
Staff::Favorites
  ログインしている場合
    DELETE /staff/ostomies/:ostomy_id/favorites
"-------------"                          ← ← ← ← ← ←ここに結果が出された
0                           ← ← ← ← ← ←ここに結果が出された
      いいね取り消す (FAILED - 1)

Failures:

  1) Staff::Favorites ログインしている場合 DELETE /staff/ostomies/:ostomy_id/favorites いいね取り消す
     Failure/Error:
               expect{
                 delete staff_ostomy_favorites_path(ostomy)
       
                }.to change { Favorite.all.count }.by(-1)
     
       expected `Favorite.all.count` to have changed by -1, but was changed by 0
     # ./spec/requests/staff/favorite_spec.rb:42:in `block (4 levels) in <top (required)>'

Finished in 0.3563 seconds (files took 2.06 seconds to load)
1 example, 1 failure

Failed examples:

let は@など使わずに済み便利な機能だが、「遅延評価される」ため expect{〜}.to change { Favorite.all.count }が実行された時にlet(:favorite) が実行されず、レコードがデータベースに保存されないためnilを返す。

before ブロックの中で明示的にlet(:favorite)をかく必要がある

favorite.spec.rb
before do
 favorite  # ここでデータベースにレコードを保存しておく
end

あるいは「  let! 」 を使うことで、実行前に let! で定義した値が作れる 遅延をなくせる

favorite.spec.rb
RSpec.describe "Staff::Favorites", type: :request do
  let(:staff){FactoryBot.create(:staff)}
  let(:ostomy){FactoryBot.create(:ostomy)}
  let(:patient){FactoryBot.create(:patient)}
  let!(:favorite){FactoryBot.create(:favorite, staff: staff, ostomy: ostomy)}
   ↑↑↑ここに↑↑↑↑[!]を入れた  let!(: )
tail -f log/test.log 
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?