LoginSignup
0
0

More than 1 year has passed since last update.

9.19振返) 要確認#テストのエラーの読み方、探し方(デバッグしてログを見る)

Last updated at Posted at 2021-09-19

教えてもらったこと

- テスト(今回の場合controller_spec)に問題があるのではないか?
 - Factoryに問題がないか?
 - pathに問題がないか?
 - paramsに問題がないか?

- 実装(今回の場合controller.rb)に問題があるのではないか?
  - ストロングパラメータに問題がないか?
  - インスタンスは作成されるか?
  - インスタンスは保存されるか?(=バリデーションに引っかからないか?)
→ binding.irbをぶちこんで見る

共通:
ログは読む。
- デバッグしながらログの流れを「ちゃんと」見る「ちゃんと!」

同じテストコードの記述を2回かましてエラーになった

今回の流れ:
今回の場合、両方(テスト&実装)を確認して特に問題がない
↓
リクエストが2回発生しているのを発見
↓
テストで2回リクエストしている
(テストの書き方の修正が必要)

ActiveSupport::MessageVerifier::InvalidSignature: また頭真っ白

ターミナルエラー部分
2) Patient::Ostomies ログインしている場合 PATCH /patient/ostomies/:id 更新する
     Failure/Error: if @ostomy.update(ostomy_params)

     ActiveSupport::MessageVerifier::InvalidSignature:
       ActiveSupport::MessageVerifier::InvalidSignature
     # /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activestorage-5.2.6/app/models/active_storage/blob.rb:47:in `find_signed'
     # /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activestorage-5.2.6/lib/active_storage/attached.rb:30:in `create_blob_from'
     # /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activestorage-5.2.6/lib/active_storage/attached/one.rb:24:in `attach'
     # /home/ec2-user/.rvm/gems/ruby-2

ログ直下にのactivestorageは画像投稿のgem

デバッグしながらログの流れを見る

画像がfactorybotやletで設定されていないため起こったとわかる


今回はバリテーションもかけてないため記述を消した

request/patient_ostomy.rb
RSpec.describe "Patient::Ostomies", type: :request do
  let(:patient) { FactoryBot.create(:patient) }
  let(:ostomy) { FactoryBot.create(:ostomy, patient: patient) }

  context 'ログインしている場合' do
    before do
      sign_in patient
    end
    let(:ostomy_params)do
    {
    patient_id: patient.id,
    color: 'pink',
    edema: 'normal',
    skin: 'same',
    h_size: '30 ',
    w_size: '30',
    comment: 'ok',
    #image: '',   #ここを消した※バリテーションもかけてないのでOK
    }
    end

画像にtrueでバリテーションかける場合はletやfacotrybotであらかじめ設定する必要がある。

# after(:build) do |dialy|
      #   dialy.attached = fixture_file_upload(Rails.root.join('spec/fixtures/images/no_image.jpeg', 'image/jpeg'))
      # end

コントローラーのテスト作成中のエラー

テストコードだけでなく、テストしている部分にもデバックする

class Patient::DialiesController < ApplicationController
 before_action :authenticate_patient!
 :
 :
  def create
       binding.irb
    @dialy = Dialy.new(dialy_params)
    @dialy.patient_id = current_patient.id
   if @dialy.save
     redirect_to patient_dialy_path(@dialy)
   else
     render :new
   end
  end

@dialy = Dialy.new(dialy_params)一個ずつ入れてbyebugする

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