Level 102
This will return 200 if this is false
return $masyarakat;
if (isset($masyarakat['data']['id'])) {
Summer Sale! All accounts are 50% off this week.
I'm trying to do testing using PHPUnit and here is my code
public function testCreate()
{
$payload = [
'vote' => true,
'id_pengaduan' => Str::random(10),
];
$result = $this->withHeaders(['Authorization' => "Bearer " . Str::random(10), 'Accept' => "application/json"])
->json('POST', 'api/vote', $payload)
->assertStatus(Response::HTTP_CREATED);
}
and it's giving me result
There was 1 failure:
1) Tests\Feature\VoteTest::testCreate
Expected response status code [201] but received 200.
Failed asserting that 201 is identical to 200.
but the result doesnt really fit my controller because I set the status code to 201 when it success and not 200. Here is my controller
public function create(Request $request)
{
$helper = new AuthHelper();
$masyarakat = $helper->Masyarakat($request->header('Authorization'));
if (isset($masyarakat['data']['id'])) {
$validateData = $request->validate([
'id_pengaduan' => 'required',
'vote' => 'required|boolean',
]);
$validateData['id_voter'] = $masyarakat['data']['id'];
$datavote = Vote::where('id_voter', $validateData['id_voter'])->where('id_pengaduan', $validateData['id_pengaduan'])->first();
if (!$datavote) {
$vote = Vote::create($validateData);
return response()->json(['message' => 'vote berhasil', 'data' => $vote], 201);
}
return response()->json(['message' => 'vote sudah ada', 'status code' => '303']);
} else {
return $masyarakat;
}
}
so I try to chech my route and I'm pretty sure I already add the correct route to my endpoint
Route::prefix('vote')->group(function () {
Route::post('/', 'VoteController@create');
});
also when I'm trying to test it without sending the $payload variable it also gives 200 when it supposed to be error.
This will return 200 if this is false
return $masyarakat;
if (isset($masyarakat['data']['id'])) {
Please or to participate in this conversation.