theblack68's avatar

Pest testing - Update

Hi... I recently started using tests in Laravel with Pest. I'm not sure I fully understand how to test the Update method.

I have a Posts Template (title, body)

I have a Controller resource

I have a route resource

The test I want to do is this:

  1. send the data through the form to the posts.update route
  2. verify that the modified record exists in the database

My doubt is this: when I submit the form, does the test insert the data directly into the database?

Or I have to verify that the form is sent and update in the test?

This is the test:

it('post can be updated', function () {  

loginAsUser('operator', 'manage-posts');
		
$post = Post::factory()->create();
   
 $post = post(route('posts.update', 1), [
      'title' => 'my title',
      'body' => 'my body post'
]); // this insert in database?
		
 $post->assertSessionDoesntHaveErrors();  

// Or maybe I need to insert manually?
$post->update(['title' => 'my title', 'body' => 'my body post']);
 
 assertDatabaseHas('posts', ['title' => 'my title']);

});

thank you for explanation ;)

0 likes
1 reply
LaryAI's avatar
Level 58

Certainly! When you're testing with Pest and Laravel, you don't need to manually insert the data into the database when you're simulating a form submission. The post function in your test will make an HTTP request to the specified route, which should trigger your controller's update method and perform the actual update operation if everything is set up correctly.

Here's how you can write the test:

it('post can be updated', function () {
    // Assuming you have a user factory and a method to log in users for tests
    $user = User::factory()->create();
    $this->actingAs($user);

    // Create a post using the factory
    $post = Post::factory()->create();

    // Send a POST request to the update route with the new data
    $response = $this->post(route('posts.update', $post->id), [
        'title' => 'Updated Title',
        'body' => 'Updated body content'
    ]);

    // Assert there are no session errors
    $response->assertSessionDoesntHaveErrors();

    // Assert the database has the updated record
    $this->assertDatabaseHas('posts', [
        'id' => $post->id,
        'title' => 'Updated Title',
        'body' => 'Updated body content'
    ]);
});

A few things to note:

  1. You should use $this->actingAs($user) to authenticate a user for your test.
  2. The post method sends the data to the specified route, which should handle the update logic.
  3. You don't need to call $post->update() manually in your test. The controller responsible for handling the posts.update route should take care of updating the post.
  4. Use assertDatabaseHas to verify that the database contains the updated record.

Make sure that your route and controller are properly set up to handle the update operation. The controller's update method should validate the request data and save the changes to the database. If everything is configured correctly, the test will pass when the database has been updated with the new post data.

1 like

Please or to participate in this conversation.