lundi 13 janvier 2020

Tests for POST request with logged user

I need to write tests for POST request. The most of them need logged user (sometimes with some permisson). I wrote this test:

    <?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use App\User;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        // $user = new User([
        //     'id' => 1,
        //     'name' => 'yisdfsdsh',
        //     'password' => 'helloWorld',
        //     'admin' => 1
        // ]);
        $user = User::find(1);
        $response = $this->actingAs($user)->withHeaders([
            'X-Header' => 'Value',
        ])->json('POST', '/change-password', ['password' => 'newpassword', 'confirmPassword' => 'newpassword']);
        $response->assertStatus(201);

        // $this->get('/')->assertStatus(200);
    }
}

But I have error:

Illuminate\Database\QueryException: could not find driver (SQL: PRAGMA foreign_keys = ON;)

I tryed too:

$user = new User([
        'id' => 1,
        'name' => 'yisdfsdsh',
        'password' => bcrypt('helloWorld'),
        'admin' => 1
    ]);

But it receive status 500. How can I wrote this test? This controller's method need to update user's password.

Aucun commentaire:

Enregistrer un commentaire