phpunit :: Laravel redirect upon validation failure

Posted 1 month ago by DawgOnKing

Please consider the following:

//controller
public function store()
    {
        $attributes = request()->validate([
            'name' => 'required',
            'title' => 'required',
        ]);
        
        Post::create($attributes);
        flash('POST CREATED!');
        return redirect('/posts');
    }
//test validation for name requirement
        $this->actingAs($admin)->post('/posts', factory(Post::class)->raw(['name' => '']))
            ->assertStatus(302)
            ->assertSessionHasErrors(['name' => 'The name field is required.'])
            ->assertRedirect('/posts/create');

The logic works in development but fails when testing:

1) Tests\Unit\PostTest::post_name_validations
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/posts/create'
+'http://localhost'

Any ideas?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.