15

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.

user3532758
  • 2,221
  • 1
  • 12
  • 17
olasammy
  • 6,676
  • 4
  • 26
  • 32

6 Answers6

58

It can be because you are using PHPUnit\Framework\TestCase instead of Tests\TestCase in your test.

Victor Timoftii
  • 2,717
  • 1
  • 16
  • 15
2

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();
}
Santiago258
  • 21
  • 1
  • 2
0

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;
    }
}
0

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),
        ];
    }
}
Eduard Brokan
  • 1,365
  • 1
  • 10
  • 8
0

In my case, I had forgotten to run the parent::setUp() function in the child class's setUp() function.

MT_Shikomba
  • 129
  • 1
  • 6
0

You need to declare "description" like paragraph, that's all. I hope this information be useful:

        'description' => $faker->paragraph
Boris
  • 602
  • 2
  • 8
  • 29