The new factories doesnt work in database testing. Error "Unknown formatter". The factory works fine in seeding etc.
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\TestCaseor use
\Tests\CreatesApplicationtrait 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
}
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.