3
3

More than 3 years have passed since last update.

単体テストエラー 〜ActiveRecord::StatementInvalid: Mysql2::Error::ConnectionError: Lost connection to MySQL server during query〜

Posted at

自分の備忘録として残しておきます

エラー内容

ActiveRecord::StatementInvalid:
Mysql2::Error::ConnectionError: Lost connection to MySQL server during query

スクリーンショット 2021-01-22 12.27.34.png

スクショを見る限り一つ目のテストは成功してるのに二つ目以降のテストは失敗している、、、なぜ?
可能性としてはテストに負荷がかかりすぎてread_timeout以内に処理が終わらなかった。

spec/models/ファイル名_spec.rb

RSpec.describe UserAddress, type: :model do
  before do
    @user = FactoryBot.create(:user)
    @item = FactoryBot.create(:item)
    @user_address = FactoryBot.build(:user_address, user_id: @user.id, item_id: @item.id)
    sleep 0.1    #この1行を追加
  end


#以下略
sleep 0.1 

上記の記述はインスタンスを生成する処理に0.1秒待機させてread_timeoutのエラーを回避するための記述です。

スクリーンショット 2021-01-22 12.59.55.png

できたぁ!(悟空)

参考記事
https://abicky.net/2017/09/17/014241/

3
3
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
3
3