Framework: [8.x] No faker formatters in database testing

Created on 17 Sep 2020  路  8Comments  路  Source: laravel/framework


  • Laravel Version: 8.2.0
  • PHP Version: 7.4.10
  • Database Driver & Version: mysql 8

Description:

The new factories doesnt work in database testing. Error "Unknown formatter". The factory works fine in seeding etc.

Steps To Reproduce:

  • create model factory and use a definition with faker
  • create an unit test
  • create an entity with the factory in the unit test
  • error "unknown formatter"

Most helpful comment

Here is the "Real" Solution
Use TestsTestCase; rather then use PHPUnitFrameworkTestCase;
Laravel ships with the later, but TestsTestCase class take care of setting up the application,
otherwise models wont be able to communicate with the database if they are extending PHPunitFrameworkTestCase.

All 8 comments

same problem

Try to extend

\Tests\TestCase

or use

\Tests\CreatesApplication

trait which boots up applications and puts dependencies to the services (including faker providers).

Standard unit test doesn't have to boot full application. This looks more like functional test to me and that's why it's not working correct way.

Try to extend

\Tests\TestCase

or use

\Tests\CreatesApplication

trait which boots up applications and puts dependencies to the services (including faker providers).

Standard unit test doesn't have to boot full application. This looks more like functional test to me and that's why it's not working correct way.

Not working

Needs to be a feature test.

If anyone is confused. The above issue was a result of this change: https://github.com/laravel/framework/commit/e30a0c979d98f2f1f7b6c565e4002734237a280b

tl;dr: The unit tests no longer use faker by default. It's recommended to use feature tests.

Here is the "Real" Solution
Use TestsTestCase; rather then use PHPUnitFrameworkTestCase;
Laravel ships with the later, but TestsTestCase class take care of setting up the application,
otherwise models wont be able to communicate with the database if they are extending PHPunitFrameworkTestCase.

I'm using Tests/TestCase but I still have this error. Can someone take a look at my code and let me know what I'm doing wrong?

<?php

namespace Tests\Feature;

use App;
use Tests\TestCase;
use App\Models\Attribute;
use Illuminate\Http\Response;

class AttributeTest extends TestCase
{
    protected $attribute;

    public function setUp(): void
    {
        $this->attribute = Attribute::factory()->translated()->make();
    }

    ...

public function setUp(): void
{
$this->attribute = Attribute::factory()->translated()->make();
}

To me it looks like you are not calling the parent setUp method:

    public function setUp(): void
    { 
         parent::setUp();
         // your code
    }

Was this page helpful?
0 / 5 - 0 ratings