iamamirsalehi's avatar

Test Can't assertRedirect

hey there. I've written a test for my update method but the assertRedirect code gives me error. Test:

   public function test_owner_can_update_his_projects()
    {
        $this->actingAs(User::factory()->create());

        $created_project = Project::factory()->create(['user_id' => auth()->user()->id]);

        $response = $this->put(route('owner.project.update', $created_project->id));

        $response->assertRedirect(route('owner.projects'));

        $response->assertSessionHasNoErrors();
    }

Update method codes:


    public function update(ProjectUpdateRequest $request, Project $project)
    {
        $validated_data = $request->validated();

        $project->update($validated_data);

        return redirect()->route('owner.projects');
    }

Error:


1) Tests\Feature\Owner\ProjectsTest::test_owner_can_update_his_projects
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/dashboard/owner/projects'
+'http://localhost'

E:\projects\tarhcadeh\vendor\laravel\framework\src\Illuminate\Testing\TestResponse.php:260
E:\projects\tarhcadeh\vendor\laravel\framework\src\Illuminate\Testing\TestResponse.php:205
E:\projects\tarhcadeh\tests\Feature\Owner\ProjectsTest.php:93


thanks in advance

0 likes
3 replies
Tray2's avatar

You are trying to do an update here but you don't pass any data along

$response = $this->put(route('owner.project.update', $created_project->id));

So most likely your validation fails and sends you back to where you were.

iamamirsalehi's avatar

@tray2 Thank you so much for mentioning, As you know put method accept an array and when I use raw method of factory to create data for me it does not create id! is there any solution for it?

Tray2's avatar

The factory create method does not create the id, it's the database that does it and the create method returns the newly created id.

1 like

Please or to participate in this conversation.