Using factories does not work as intended in tests. Trying to call a factory results in the following error:
Fatal error: Uncaught Error: Call to a member function define() on null
This is for orchestra v3.8
These are the TestCase, Test and factory files being used.
namespace Tests;
use Orchestra\Testbench\Concerns\WithFactories;
use Orchestra\Testbench\TestCase as BaseTestCase;
class TestCase extends BaseTestCase
{
use WithFactories;
protected function setUp(): void
{
parent::setUp();
$this->withFactories(__DIR__.'/../src/database/factories');
}
protected function getPackageProviders($app): array
{
return [
// providers
];
}
protected function getPackageAliases($app)
{
return [
// aliases
];
}
}
namespace Tests\Feature;
use App\User;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
class UserTest extends TestCase
{
use WithFaker;
private $user;
public function setUp(): void
{
parent::setUp();
$this->artisan('migrate');
$this->user = factory(User::class)->create();
}
}
/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\User;
use Illuminate\Support\Str;
use Faker\Generator as Faker;
$factory->define(User::class, function (Faker $faker) {
return [
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '', // password
'remember_token' => Str::random(10),
];
});
Can you tell me what could possibly be wrong here? Is this an implementation error or a bug in orchestra itself?
question