Laravel 5.2: Unable to locate factory with name [default]
Some times it could be due to importing the wrong TestCase
use PHPUnit\Framework\TestCase;
[WRONG: and throws this error]
use Tests\TestCase;
[CORRECT]
If nothing helps with PHPUnit.
For those of readers who stuck with the same issue in tests, I found out that I forgot to add parent::setUp()
in setUp
method.
By default the laravel installation comes with this code in the database/factories/ModelFactory.php
File.
$factory->define(App\User::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'email' => $faker->email,
'password' => bcrypt(str_random(10)),
'remember_token' => str_random(10),
];
});
So you need to define a factory Model before you use it to seed database. This just uses an instance of Faker Library which is used to generate fake Data for seeding the database to perform testing.
Make sure You have added a similar Modal Factory for the Comments Model.
So your Comments Model Factory will be something like this :
$factory->define(App\Comment::class, function (Faker\Generator $faker) {
return [
'comment' => $faker->sentence,
// Any other Fields in your Comments Model
];
});
This can also happen when you are running the command factory()->create()
from php artisan tinker
. Make sure you save the file database/factories/ModelFactory.php
before opening tinker