Answer the question
In order to leave comments, you need to log in
How to write phpunit api tests in laravel?
There is a method in the controller, please tell me how to write a test for it, I can’t understand where to start and what should happen in the end, please explain using my example.
public function addResource(Request $request)
{
$url = $request->input('url');
$result = $this->service->saveContent($url);
if ($result !== null) {
return response()->json(['status' => 'ok', 'message' => 'Ресурс добавлен', 'data' => $result]);
}
return response()->json(['status' => 'error', 'message' => 'Ошибка', 'data' => ['url' => $url]]);
}
public function testBasicExample()
{
$response = $this->json('POST', '/api/resource', ['name' => 'saveContent']);
$service = $this->createMock(Request::class);
$contentController = new ContentController($service);
}
Answer the question
In order to leave comments, you need to log in
1) You need to test requests to api, in this case you just check the response like this
$response = $this->post('/api/upload');
$response
->assertStatus(200)
->assertExactJson([
'code' => 200,
'output' => [],
]);
;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question