1
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 1 year has passed since last update.

Laravel6 Unitテスト時に「Maximum function nesting level~」のエラーが出た

Last updated at Posted at 2020-11-27

目的

  • しょーもないミスで時間を費やしてしまったので反省の意味も含めてエラー原因などをメモ的にまとめる

前提情報

  • LaravelのUnitテストを実行したところ「Error: Maximum function nesting level of '256' reached, aborting!」というエラーが出た。
  • テストではServiceを呼び出し、紐付いたRepositoryからデータを取得する事ができるかを確認していた。

原因

  • Serviceクラスの依存注入でそのServiceクラス自体を依存注入してしまっていた。

  • さらにServiceクラス内のメソッドでそのメソッドを呼び出す処理を記載していた。

  • わかりにくいと思うので下記に図を記載する。

    • 理想

      mermaid-diagram-20201127145744.png

    • 今回

      mermaid-diagram-20201127145729.png

  • 上記でなんとなくご理解いただけたと思うがUnitテストで呼び出しているServiceクラスのメソッドが自身を呼び出すようになってしまっていた。

  • 当該ループが256回に達したため、エラーを出力して処理が停止した。

改善策

  • ServiceクラスでRepositoryクラスが依存注入できているかを確認する。

  • 下記画像の流れを意識しながら実装する。

    mermaid-diagram-20201127145744.png

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