Answer the question
In order to leave comments, you need to log in
How to make tests saved in the test database?
I conduct tests, with confirmation, only I need data to be saved by rest not to the main database, but to the test one, how can this be implemented approximately?
My implementation
/**
* Успещная регистрация
*/
public function testRegisterSuccess()
{
$response = $this->getPathSuccess('POST', '/api/register', ['email' => '[email protected]', 'password' => 'test123']);
$response->assertJsonStructure([
'success',
'error',
'response' => ['message'],
'message'
]);
}
/**
* Проверяет на успешность запроса на регистрацию
* @param $method
* @param $url
* @param $data
*
* @return \Illuminate\Foundation\Testing\TestResponse
*/
protected function getPathSuccess(string $method, string $url, array $data)
{
return $this->json($method, $url, $data)
->assertStatus(200)
->assertHeader('Content-type', 'application/json;charset=utf-8');
}
public function register(ApiRegister $request)
{
$request->validated();
try {
User::create([
'name' => $request->input('name'),
'email' => $request->input('email'),
'password' => Hash::make($request->input('password'))
]);
return ResponseApi::response(['message' => 'Пользователь создан']);
} catch (\Exception $exception){
return ResponseApi::response([], false, 500, ['errors' => 'Произошла ошибка']);
}
}
'testing' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'test_database'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
APP_ENV=testing
DB_CONNECTION=testing
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=test_database
DB_USERNAME=implant_user
DB_PASSWORD=kazan1811
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="testing"/>
php artisan migrate --database=testing --env=testing
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question