Summer Sale! All accounts are 50% off this week.

russellxu's avatar

Unknown formatter "paragraph" when unit testing

I was learning Let's Build A Forum with Laravel and TDD. There's an error occured when i do the unit test.

function it_has_an_owner(){
    $reply = Reply::factory()->create();

    $this->assertInstanceOf('App\Models\User', $reply->owner);
}

There was 1 error:

  1. Tests\Unit\ReplyTest::it_has_an_owner InvalidArgumentException: Unknown formatter "paragraph"

D:\work\laracasts_forum\vendor\fakerphp\faker\src\Faker\Generator.php:250 D:\work\laracasts_forum\vendor\fakerphp\faker\src\Faker\Generator.php:230 D:\work\laracasts_forum\vendor\fakerphp\faker\src\Faker\Generator.php:287 D:\work\laracasts_forum\database\factories\ReplyFactory.php:33 D:\work\laracasts_forum\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Factories\Factory.php:380 D:\work\laracasts_forum\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Factories\Factory.php:359 D:\work\laracasts_forum\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Factories\Factory.php:343 D:\work\laracasts_forum\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\GuardsAttributes.php:157 D:\work\laracasts_forum\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Factories\Factory.php:348 D:\work\laracasts_forum\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Factories\Factory.php:316 D:\work\laracasts_forum\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Factories\Factory.php:228 D:\work\laracasts_forum\tests\Unit\ReplyTest.php:15

And the Reply factor is:

class ReplyFactory extends Factory { /** * The name of the factory's corresponding model. * * @var string */ protected $model = Reply::class;

/**
 * Define the model's default state.
 *
 * @return array
 */
public function definition()
{
    return [
        'thread_id' => function(){
            return Thread::factory()->create()->id;
        },
        'user_id' => function(){
            return User::factory()->create()->id;
        },
        'body' => $this->faker->paragraph,
    ];
}

}

Why is this happening?

0 likes
10 replies
languafe's avatar

Seeing this exact thing myself and haven't figured out why! Also trying to follow along the Let's Build a Forum series while using Laravel 8.

languafe's avatar

This seems to do the trick: replace use PHPUnit\Framework\TestCase; with use Tests\TestCase;!

5 likes
languafe's avatar

To note, I created the unit test with php artisan make:test ReplyTest --unit exactly the same way Jeffrey did in the video.

languafe's avatar

I used the following command to generate the unit test, exactly like Jeffrey did in the video:

php artisan make:test ReplyTest --unit

This creates the tests/Unit/ReplyTest.php file with the following contents:

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase; // change this to use Tests\TestCase; to fix issue discussed in this conversation

class ReplyTest extends TestCase
{
    /**
     * A basic unit test example.
     *
     * @return void
     */
    public function testExample()
    {
        $this->assertTrue(true);
    }
}

I am currently using Laravel Framework 8.13.0 (check by running php artisan -V.

If you generate a feature test instead (by omitting the --unit flag), the resulting test class extends Tests\TestCase instead of PHPUnit\Framework\TestCase.

2 likes
automica's avatar

@languafe

If you generate a feature test instead (by omitting the --unit flag), the resulting test class extends Tests\TestCase instead of PHPUnit\Framework\TestCase.

helpful to know. Thanks

automica's avatar

@russellxu Its not much of a manual change. I guess the take home here is that if you want to use faker then done create the test with --unit flag.

TBH as Tests\TestCase extends PHPUnit anyway, perhaps just use 'use Tests\TestCase' for all tests.

2 likes
Overflow394's avatar

In my case i just forgot to call parent::setUp() on my setUp() method. Generally, you should call parent::setUp() at the first line of your setUp() method to let the parent class (TestCase) work as expected.

Please or to participate in this conversation.