samedi 20 février 2021

testing the relationship opun creating a model in laravel

in my registration process if some setting is active i add a realtionship to user model called Vendor so if the setting is active when user is being created a model (relationship) will be created for him too . now what i want to do is to write a test for it that if the setting is on and a user is created can i see the relation in database or not . what i have wrote so far is that :

        $response = $this->call('POST', 'api/user/sign-up', ['name'=>'test','password'=> '123456','password_confirmation'=>'123456']);
    $response->assertStatus(201);
        $response->assertDatabaseHas('users',['name'=>'test']);
        $response->assertDatabaseHas('user_vendor',['user_id'=>'Cantfinduserid']);

but because i am not returning the user in api i cant find the userid to assert the database has this the other way i wrote it was :

        $shop = factory(Shop::class)->create();
 $response->assertStatus(201);
        $response->assertDatabaseHas('users',['name'=>'test']);
        $response->assertDatabaseHas('user_vendor',['user_id'=>'Cantfinduserid']);

but i dont know in second way the relation is doing to be created or not because i am making it in my user controller . now i really dont know how to test this . thanks for your help

Aucun commentaire:

Enregistrer un commentaire