LoginSignup
0
1

More than 1 year has passed since last update.

RSpecのテスト中にMySQL client is not connected

Posted at

テストコード記述中にエラーが発生。

Failure/Error: _query(sql, @query_options.merge(options))

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

エラーの内容としてはクエリ中にMySQLとの接続が切れた、という内容ですね。
特に今回はAPIを使用したテストコードなので、高速でテストをしすぎているのが原因みたい。

ということで、sleepを導入する。

spec/models/purchase_shipping_spec.rb
RSpec.describe PurchaseShipping, type: :model do
  describe '購入情報の作成' do
    before do
      item = FactoryBot.create(:item)
      user = FactoryBot.create(:user)
      @purchase_shipping = FactoryBot.build(:purchase_shipping, item_id: item.id, user_id: user.id) 
      sleep(1) #追記
    end

これでFactorybotで作成した情報ができたら、1秒待機することになり、MySQLも正常に読み込みます。
以前Pythonでスクレイピングするときにも似たようにSleepを入れて意図的に処理を遅らせていましたが、それと似てますね。

0
1
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
1