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

codingstuff91's avatar

Update testing failed and i dont know why

Hello the community of Lara addicts ^^

I request your help because i am stuck on a testing problem.

On my application, i have to manage some "Project", so i did some automated tests to ensure the features work correctly (the create and the update features)

But when i want to test the update a project feature, for an unknow reason the update method of my "ProjectController" doesnt work and i dont understand why... it seems that the project is not edited by the update method called in my test and i dont know why.

Besides, it is very strange, because the real feature works correctly so could you tell me what i did wrong or what i forgot ?

Here is the route is used for updating a project :

PUT|PATCH | admin/projects/{project}

Here is the Update method in my controller "ProjectController" :

public function update(UpdateProjectRequest $request, Project $project)
{
    $project->update([
        'name' => $request->name
    ]);

    return redirect()->route('admin.index');  
}

And finally here is my test :

public function test_a_project_can_be_updated()
{
    $this->setup_test_db();
    
    $project = Project::first();
    
    // Mise à jour du projet
    $response = $this->patch('/admin/projects/'.$project->id, [
        'name' => 'NOUVEAU TITRE'
    ]);
            
    $project = Project::first();

    $this->assertEquals('NOUVEAU TITRE', $project->name);
}
0 likes
16 replies
tisuchi's avatar

@codingstuff91 What issue you are exactly facing?

BTW, have you created the project entry before trying to update it? In your test, I cannot see that you have created any project entry.

1 like
codingstuff91's avatar

@tisuchi Sorry i forgot to explain that ^^

The problem i'm facing is that the project is not updated by the update method of the ProjectController and the test fails. This behaviour is quite strange because this behavior works fine on the real application ^^

Indeed i'm sure that a project is really created because i tested it with some dd stuff. Its the line $this->setup_test_db(); that create a dummy project for my test

tisuchi's avatar

@codingstuff91 It's hard to predict what's going on.

But can you show the setup_test_db() method and your route?

codingstuff91's avatar

Here is the setup_test_db method (it seeds a fake database) :

protected function setup_test_db()
{
    $this->seed();
}

The route for updating the project is this (its a resource route controller) :

PUT|PATCH | admin/projects/{project}

Sinnbeck's avatar

Try adding $this->withoutExceptionHandling() at the start of the test (before the request)

Maybe validation fails?

1 like
codingstuff91's avatar

@Sinnbeck I tried your solution but the problem is still there.

The validation is not failing because im just using a formRequest that verifies that the field "name" is not empty

I tried to put a dd before and after the update and i realised that the update method on my Eloquent model did nothing

codingstuff91's avatar

Hi,

When i do a dd after selecting the first i get the original Project model instance :

#attributes: array:3 [ "id" => "1" "name" => "RISE OPEN FAN" "image_path" => "rise_open_fan.png"

But when i do another dd after the update i realise that the project has not been updated because i get the exact same result :

#attributes: array:3 [ "id" => "1" "name" => "RISE OPEN FAN" "image_path" => "rise_open_fan.png"

codingstuff91's avatar

The fillable property is correct :

protected $fillable = ['name', 'image_path'];

I ll try to use factory instead of seeders to see if there is another result

axeloz's avatar

@codingstuff91 I would rather use

$this->assertDatabaseHas('projects', [
'title' => 'NEW TITLE',
... # other fields you'd like to validate if applicable
];

This will check what is actually stored in the DB

1 like
codingstuff91's avatar

I tried to use Factory instead of a Seeder and the problem is still here and i dont understand why...

The Project Model isnt updated even if i use the correct route to update, it makes me feel so enraged... GRRR

For the moment i give up about the idea of testing the update process, because i know it works on the real application.

codingstuff91's avatar

I tried many solutions but nothing works.

i check with a "dd" the $response object and the status code is 200 !!!

The another weird thing, is that i check the $request->name received by the update method and it contains the real new value i use to update my project.

I checked also the validation and the formRequest associated is correct

So the main question is still "Why my update doesnt work ?????'"

codingstuff91's avatar

Hourrah i found the root cause of my very strange problem.

It seems that the route for updating one Project model doesnt get correctly the instance of the Project model even if i pass it correctly via a correct route model binding.

However i do it properly and i dont understand why this feature work on the real app and not in test... AAAAAH it makes really MAAAAD !!

I try to reach a Project Model with a basic $project = Project::find(1) and with that the update worked !!

codingstuff91's avatar

Allelujah !!!! I have finally find where my problem was. The root cause of my updating testing problem was due to the Trait "WithoutMiddleware" that i put on the top of my test. I deleted this trait and the update worked correctly. I give the solution if someone has the same issue as me. Thanks a lot everybody for all your answers

Please or to participate in this conversation.