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

Shivamyadav's avatar

InvalidArgumentException: Unknown format "sentence"

My factory code

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Project>
 */
class ProjectFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'title' => $this->faker->sentence,
            'description' => $this->faker->sentence,
        ];
    }
}

my migration

 $table->string('title');
            $table->string('description');

wanted to run this test it shows me this error

 public function test_it_has_a_path()
    {
        
        $project = Project::factory()->create();
        $this->assertEquals('/projects/' . $project->id, $project->path());
    }
0 likes
11 replies
tykus's avatar

Make sure your Unit Test extends the Tests\TestCase class; not the PHPUnit `TestCase class

2 likes
Shivamyadav's avatar

@tykus yeah sure!

class ProjectTest extends TestCase
{
    use RefreshDatabase;
    /**
     * A basic unit test example.
     *
     * @return void
     */
    // /** @test */
    public function test_it_has_a_path()
    {
        
        $project = Project::factory()->create();
        $this->assertEquals('/projects/' . $project->id, $project->path());
    }
}
Shivamyadav's avatar

@tykus running it by this command vendor/bin/phpunit tests/Unit/ProjectTest.php

Shivamyadav's avatar

@tykus here it is

<?php

namespace Tests\Unit;

use App\Models\Project;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\TestCase;

class ProjectTest extends TestCase
{
    use RefreshDatabase;
    /**
     * A basic unit test example.
     *
     * @return void
     */
    // /** @test */
    public function test_it_has_a_path()
    {
        
        $project = Project::factory()->create();
        $this->assertEquals('/projects/' . $project->id, $project->path());
    }
}
Shivamyadav's avatar

@tykus done now getting this error

 Tests\Unit\ProjectTest::test_it_has_a_path
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'/projects/1'
+'/projects/{ 1}'
tykus's avatar

@Shivamyadav well, that's a different problem, isn't it! How is the path method implemented?

tykus's avatar
tykus
Best Answer
Level 104

@Shivamyadav this should work - note the space is removed and ensure there are double quotes (") used:

public function path()
{
    return "/projects/{$this->id}";
}
1 like

Please or to participate in this conversation.