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,

I have a problem with my test. There are duplication of datas and I don't know why.

I have these error below :

F............                                           23 / 23 (100%)

Time: 8.36 seconds, Memory: 24.00 MB

There was 1 failure:

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

[
    "The title has already been taken.",
    "The thumbnail must be an image.",
    "The material field is required.",
    "The material.0 field is required."
]
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:107
/home/audrey/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:201
/home/audrey/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:160

FAILURES!
Tests: 23, Assertions: 71, Failures: 1.

Can you help me please with that ?

there are all code at this link : https://codeshare.io/aJY79q

Thank you by advance.

0 likes
14 replies
aurawindsurfing's avatar

Hey @browniecoffee

For starters you could do simply this:

 $project = factory(Project::class)->make([
            'title' => 'Ceci est un nouveau titre',
            'user_id' => factory(User::class)->create()->id(),
            'category_id' => factory(Category::class)->create()->id(),
            'material_id'  =>factory(Material::class)->create()->id()
        ]);

instead of this:

$user = factory(User::class)->create();
            $project = factory(Project::class)->make([
                'title' => 'Ceci est un nouveau titre'
            ]);
            $cat = factory(Category::class)->create();
            $material = factory(Material::class)->create();



            // dd($mat1, $mat2);

            //relation between user and projects 
            $user->projects()->save($project);


            // relation between project and user
            $project->user()->associate($user)->save();


            //relation between project and material
            $project->materials()->save($material);

             //relation between material and project

            $material->project()->associate($project)->save();



            //relation between project and category
            $project->category()->save($cat);

            //relation between category and project
            $cat->categorizable()->associate($project)->save();

looks much better IMHO. It will create all the relationships necessary. Not sure if you even need the ->id() part of the factory. It will probably work without it.

What exception or error are you getting exactly?

Lenophie's avatar

Hi,

When testing, the data gets stored in your database, if you don't clean it between tests or work with transactions, you will get similar errors when running the test for a second time and ever after.

You may want to setup a testing-specific environment and setup database refreshing or transactions, you will get all the info you need here https://laravel.com/docs/5.8/database-testing#resetting-the-database-after-each-test and here https://laravel.com/docs/5.8/configuration#environment-configuration

Know that the trait DatabaseTransactions exists as well but isn't documented.

However, the setup is special when using Laravel Dusk : https://laravel.com/docs/5.8/dusk#migrations

Hope this helps :)

BrownieCoffee's avatar

@aurawindsurfing , @lenophie hi guys.

The solution given by @aurawindsurfing seems like good but I've still errors for thumbnail field.

//actual error


.........."86188d512f07f6c9773f1090ff1e6aeb.jpg"
F.............                                          24 / 24 (100%)

Time: 30.51 seconds, Memory: 26.00 MB

There was 1 failure:

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

[
    "The thumbnail must be an image.",
]
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:67
/home/audrey/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:201
/home/audrey/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:160

FAILURES!
Tests: 24, Assertions: 71, Failures: 1.

// my request 

  public function rules()
    {
        return [
            
        'category' => 'required',
        'title' => 'required|unique:projects,title|min:10|max:80',
        'thumbnail' =>'nullable|image|max:5000',
        'material' => 'required|array|min:1', // je précise ici que "material" doit être un tableau,  qu'il doit être requis et  qu'il doit compté au moins 1 élément.
        'material.*' => 'nullable|alpha_num|between:3,200|distinct', // je dis que pour tous les valeurs dans ce tableau peuvent être nulles, de type string, faire au minimum 3 caractères et unique
        'material.0' =>'required', // je dis que le champ 0 doit être requis
        'content' => 'required|min:10'

        ];

    }
actual code test


 /**
     * Fields are filled.
     *
     * @return void
     * @test
     */
    public function the_fields_are_filled()
    {
         
        $project = factory(Project::class)->make([
            // 'title' => 'Ceci est un nouveau titre',
            'user_id' => factory(User::class)->create(),
            'category_id' => factory(Category::class)->create(),
            'material_id'  =>factory(Material::class)->create()
        ]);

        dump($project->thumbnail);



        $response =  $this->actingAs(factory('App\User')->make())->post(route('projets.store'),
              [
                    'category' =>$project->category_id,
                    'user_id' => $project->user_id, 
                    'title' => $project->title, //pb -> titre déjà pris
                    'thumbnail' =>$project->thumbnail, // pas de thumbnail
                  //  'material[]' => $project->material_id, // pas de materiels
  'material' => [  // <===== EDIT
                        $project->material_id
                    ] , // pas de materiels
                    'content' => $project->content
                
                    ]
               
                ); 
        

        $response->assertSessionHasNoErrors()->assertEquals(1,Project::all()->count());
    }   


Do you know why I have thoses errors... ?

BrownieCoffee's avatar

@aurawindsurfing Sorry I didn't understood your question.

<?php

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

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


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


aurawindsurfing's avatar

For the thumbnail add this to your validator rules:

'image|mimes:jpeg,png,jpg,gif,svg|'

You are failing validation at not providing mime types

BrownieCoffee's avatar

@aurawindsurfing HI . I tryed your solution but I have still error.

I just tryed with this piece of code too image|mimetypes:image/jpeg,image/png,image/jpg but in vain...

aurawindsurfing's avatar

remove that piece and see if this is where the problem is. Then just play with it until you get it correct. This line is cousing your error IMHO.

BrownieCoffee's avatar

@aurawindsurfing

I remove image|mimes:png,jpg,jpeg| and it's working. I don't know why. I check my form and There is enctype="multipart/form-data" ...

aurawindsurfing's avatar

It is failing correctly as it does not find an image. The one you generate with $faker does not pass validation. Maybe hardcode it to one that actually exists in your project.

Please or to participate in this conversation.