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.

Mysql2::Error: MySQL client is not connectedというエラーについて

Last updated at Posted at 2021-08-15

はじめに

本記事は、Mysql2::Error: MySQL client is not connectedというエラーが起こり、
それを解決するための対策を記述しています。

Mysql2::Error: MySQL client is not connected

テストを実行するのに、負荷がかかったことが原因でした。

小さい処理だった場合でも、
mysqlの接続スピードよりテストの処理が実行されるスピードの方が早いためエラーが起きています。
(自宅のWi-Fiがちょっと...というやつでした。)

結論

order_form_spec.rb
RSpec.describe OrderForm, type: :model do
  before do
    user = FactoryBot.create(:user)
    item = FactoryBot.create(:item)
    @order_form = FactoryBot.build(:order_form, user_id: user.id, item_id: item.id)
    sleep 0.1
  end
# 以下省略
# これを入力してください。
sleep 0.1

処理の途中0.1秒とめるというイメージです。

当時の状況

テストの記述が終わりましたので、
bundle exec rspec spec/models/order_form_spec.rbしましたところ

ActiveRecord::StatementInvalid:
Mysql2::Error: MySQL client is not connected

というエラーが発生しました。
下行には、

Finished in 0.29523 seconds (files took 1.3 seconds to load)
13 examples, 9 failures

なるほど。
と念の為、もう一度テストコマンド入力すると、

13 examples, 4 failures

!?
たまにエラーが0の時があり、テスト成功します。

13 examples, 0 failures

マリオパーティーしている感覚でした。

 終わりに

テスト完璧!と思っていたため、かなり焦りました。
テストができていないと、アプリケーションとしても不十分であることの可能性がですからね。
あと、Wi-Fi変えようかな・・・。

他にも、テストに関する記事を投稿しているので参考にしてください。
[テスト]FactoryBotにimageを記述する

今回の参考記事です。
RSpecのテスト中にMySQL client is not connected

明日から週はじめ!
頑張っていきましょう!!

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?