vendredi 8 mars 2019

How Test Relationship with Factory "make" Laravel

Hello I have one Test in my app:

public function testCreate(){
   $userAdmin = factory(User::class)->make();
   $roleAdmin = Role::where('name', 'admin')->first();
   $userAdmin->roles()->attach($roleAdmin);

   $this->actingAs($userAdmin)->post('/api/object/create', [])
      ->seeJson(['created' => true,
   ]);
 }

I want use factory(User::class)->make() because I don't want create real data on my DB. But when I call :

 $userAdmin->roles()->attach($roleAdmin);

I return this error:

There was 1 error:

1) Tests\Feature\MyTest::testCreate Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null (SQL: insert into role_user (role_id, user_id) values (2, ))

And could be reasonable because it doesn't creates real data in DB, but so:

How can simulate attach without save in my DB?

Thanks all.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire