6
3

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 5 years have passed since last update.

LaravelのfactoryでInvalidArgumentException: Unable to locate factory with name [default] [hogehoge]. が出た

Last updated at Posted at 2020-03-29

はじめに

上記でググってきた、ありがちな 'hogehoge' の参照方法が間違っていたわけではなかった。

結論から書くと

php artisan make:test hogeTest --unit でUnitテストを生成したときに
use PHPUnit\Framework\TestCase; でUnitテストが作成されていたので
use Tests\TestCase; と書き直した

考察

「そもそも今までUnitテスト内でfactoryを使ってたのになぜ急に!」
と思い、今までのと比較してみたら上記が違いましたよ、と。
検証してはないけど、Laravelのバージョンを5系から6系に変えたのでそのタイミングで変わったのかもしれない。

また本来、 use PHPUnit\Framework\TestCase; でもfactoryを呼び出せるようにすることが正しい解決方法だとは思う。

https://qiita.com/kawagashira/items/30b6fbd7a865f4005a90
現状結論の解決方法で良かった。
しかし単体テストを名乗ってはいけなかった。

追記

https://laravel.com/docs/5.8/testing
https://laravel.com/docs/6.x/testing

を比較するとやはりバージョンアップが関わっているようだ。が。factory使えないのはなんでだろう?

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?