Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

BrownieCoffee's avatar

Session has unexpected errors:

Hello again.

I have a problem for my second test. I trying to test, for this time, that all my fields are filled.

I have this error in my console:

PHPUnit 7.5.13 by Sebastian Bergmann and contributors.

..........F.............                                          24 / 24 (100%)

Time: 4.55 minutes, Memory: 28.00 MB

There was 1 failure:

1) Tests\Feature\ProjectCreationTest::the_fields_are_filled
Session has unexpected errors: 

[
    "Il semblerait que ce titre soit déjà pris",
    "Le titre doit est composé d'au moins 10 caractères",
    "The thumbnail must be an image.",
    "The material field is required.",
    "Vous devez inscrire au moins un matériel pour ce projet"
]
Failed asserting that true is false.

/var/www/html/goshr/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:991
/var/www/html/goshr/tests/Feature/ProjectCreationTest.php:68
/home/audrey/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:201
/home/audrey/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:160


My factory seems to be O.K.

<?php

/* @var $factory \Illuminate\Database\Eloquent\Factory */
use App\Project;
use Faker\Generator as Faker;

$factory->define(Project::class, function (Faker $faker) {


    return [
        'user_id' => function () {
            return factory(App\User::class)->create()->id;
        },
        'title' => $faker->word(4),
        'thumbnail' =>$faker->image('public/images/',640,480, null, false),
        'material_id'=> function () {
            return factory(App\Material::class)->create()->id;
        },
        'content' => $faker->text()
    ];
});

and my test too


    /**
     * Fields are filled.
     *
     * @return void
     * @test
     */
    public function the_fields_are_filled()

    {


    $user = factory(User::class)->make();


    $projects = factory(Project::class, 3)
           ->create()
           ->each(function ($project) {
                $project->category()->save(factory(Category::class)->make());
            });


        foreach($projects as $project){
            
            $response =  $this->actingAs($user)->post(route('projets.store'),[
            'category' => $project->category->id ,
            'user_id' => $project->user_id,
            'title' => $project->title,
            'thumbnail' =>$project->thumbnail,
            'material_id' => $project->material_id,
            'content' => $project->content
             
        ]);
        }

        

        $response->assertStatus(302)->assertSessionHasNoErrors();

    }

I'm not sure but I'm think my loop is the problem, but it seems to be neccesary.

Do you know why my test isn't true ?

Thank by advance.

0 likes
5 replies
aurawindsurfing's avatar

Hi @browniecoffee

Disable error handling:

$this->withoutExceptionHandling();

Try to break assertions in 2 separate ones. Comment them out and see which one does not work.

$response->assertStatus(302);
$response->assertSessionHasNoErrors();

You are only testing the last project with this piece of code:

foreach($projects as $project){

            $response =  $this->actingAs($user)->post(route('projets.store'),[
                'category' => $project->category->id ,
                'user_id' => $project->user_id,
                'title' => $project->title,
                'thumbnail' =>$project->thumbnail,
                'material_id' => $project->material_id,
                'content' => $project->content

            ]);
        }

        $response->assertStatus(302)->assertSessionHasNoErrors();
BrownieCoffee's avatar

@aurawindsurfing hi. I have this error.

1) Tests\Feature\ProjectCreationTest::the_fields_are_filled
Illuminate\Validation\ValidationException: The given data was invalid.

I read your last sentence: "You are only testing the last project with this piece of code:"

I not sure to understand... how I can do ?

aurawindsurfing's avatar
Level 50

Hey @browniecoffee

Illuminate\Validation\ValidationException: The given data was invalid

Means it is failing at your validation. It does not pass your validation.

Please or to participate in this conversation.