lundi 23 mars 2020

Problem with testing method with actingAs

I have this code:

/**  @test */
public function testBasicExample()
{
    $user = User::find(1);
    // $user = factory(User::class)->create();
    $response = $this->actingAs(User::find(1))->json('POST', '/store/ad', [
                'title' => 'Hello World',
                'city' => 1,
                'phone' => '666555555',
                'description' => 'fasd asd as d asd as d asd as d asd as d asd as d asd as da sd asd',
                'user_id' => 1
    ]);

    $response
        ->assertStatus(201)
        ->assertJson([
            'created' => true,
        ]);
}

Unfortunatly at this moment I have a first problem. It couldn't see users table.

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: users (SQL: select * from "users" where "users"."id" = 1 limit 1)

I'm looking how I can solve my problem and I found that I must using DatabaseMigrations. So I add that

use Illuminate\Foundation\Testing\DatabaseMigrations;
class ExampleTest extends TestCase
{
    use DatabaseMigrations;
//...
}

But now I have new problem.

TypeError: Argument 1 passed to Illuminate\Foundation\Testing\TestCase::actingAs() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given

So I implement that

use Illuminate\Contracts\Auth\Authenticatable;
class ExampleTest extends TestCase
{
    use DatabaseMigrations;
    use Authenticatable;
//...
}

It's generated new error:

Tests\Feature\ExampleTest cannot use Illuminate\Contracts\Auth\Authenticatable - it is not a trait

How can I solve my problem? How can I testing that?

Aucun commentaire:

Enregistrer un commentaire