Reputation: 7376
I have tried everything possible but I could not get to the bottom of what I am doing wrong. I am trying to load my database with dummy data but I keep get unknown formatter "description". Description is one of the variables I am using.
Below is my factory code and my seeder coder
use Faker\Generator as Faker;
use Analytics\Blockgrant;
$factory->define(Blockgrant::class, function (Faker $faker) {
return [
'description' => $faker->description,
'value' => $faker->value
];
});
<?php
use Faker\Generator as Faker;
use Universityobfanalytics\Blockgrantcomponents;
$factory->define(Blockgrantcomponents::class, function (Faker $faker) {
return [
'blockgrants_id' => $faker->blockgrants_id,
'description' => $faker->description,
'percentage' => $faker->percentage,
'value' => $faker->value
];
});
<?php
use Illuminate\Database\Seeder;
use Analytics\Blockgrant;
use Analytics\Blockgrantcomponents;
class BlockgrantSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(Blockgrant::class, 10)->create()->each(function ($blockgrant) {
$blockgrant->blockgrantcomponents()->save(factory(Blockgrantcomponents::class)->create());
});
}
}
I am using a one to one hasOne
and belongsTo
relationship
Can somebody please assist by telling me what I am doing wrong.
Upvotes: 19
Views: 18790
Reputation: 615
You will face this issue when using Factories in PhpUnit DataProvider. As the DataProdiver is called before the test suite is properly bootstraped, the faker is not yet initialized. To get around this issue use the callbacks:
//...
public static function provideDifferentModelStates()
{
return [
'foo' => [
fn() => MyModel::factory()->foo()->make(),
],
'bar' => [
fn() => MyModel::factory()->bar()->make(),
],
'both' => [
fn() => MyModel::factory()->foo()->bar()->make()
]
];
}
#[DataProvider('provideDifferentUpdates')]
public function testMyModelStates(callable $parameters)
{
$model = $parameters();
// your code and assertions
}
//...
Upvotes: 0
Reputation: 101
You need to declare "description" like paragraph, that's all. I hope this information be useful:
'description' => $faker->paragraph
Upvotes: 0
Reputation: 187
In my case, I had forgotten to run the parent::setUp()
function in the child class's setUp()
function.
Upvotes: 5
Reputation: 1463
Solution 1 - if use tests
Check in test setUp
, do you call parent::setUp()
.
protected function setUp(): void
{
parent::setUp();
$this->setUpBannedUsersList();
}
Solution 2 Try create and use new faker:
$faker = \Faker\Factory::create();
Example
class UserFactory extends Factory
{
protected $model = User::class;
public function definition(): array
{
//Instead of using $this->faker use new created $faker
$faker = \Faker\Factory::create();
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => Hash::make($faker->password),
];
}
}
Upvotes: 1
Reputation: 420
You need to create your own TestCase class, which will use the CreatesApplication trait you created.
All this will look like this:
class TestCase extends BaseTestCase
{
use CreatesApplication;
}
trait CreatesApplication
{
public function createApplication(): Application
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
return $app;
}
}
Upvotes: 0
Reputation: 31
I am using Pest and was getting the same error when trying to write unit tests in my project, my solution:
In the file tests/Pest.php
, I had a line like this:
uses(Tests\TestCase::class)->in('Feature');
I changed it to this and it worked
uses(Tests\TestCase::class)->in('Feature', 'Unit');
In the classTest/TestCase
, remember to call parent::setup()
in the constructor, like this:
protected function setUp(): void
{
try {
parent::setUp();
} catch (QueryException $e) {
file_put_contents(
getcwd() . '/testing_env_log',
json_encode([
'db' => Config::get('database'),
'env' => $_ENV,
'app' => Config::get('app'),
], JSON_THROW_ON_ERROR)
);
throw $e;
}
Event::fake();
}
Upvotes: 3
Reputation: 3308
It can be because you are using PHPUnit\Framework\TestCase
instead of Tests\TestCase
in your test.
Upvotes: 75