dimanche 29 mars 2020

How to create mock model in laravel testing

I need to test a function in UserController :

public function CreateUser(Request $request): Response
{    
    $user = User::firstOrCreate(['device_id' => $request->device_id]);
    $token = Auth::login($user);
    return Response(['status'=> 'user created successfully'],200);
} 

and I create a test function same as following:

public function testLoginGuest()
{
    $mockUser = Mockery::mock(new App\User());
    $this->app->instance(App\User::class, $mockUser);
    $this->post(route('user_create'), ['device_id' => 'REC00ER']);
    ...
}

but this function create a real row in database. how to i can mock database for this request ?

Aucun commentaire:

Enregistrer un commentaire