I am testing my api routes in my laravel app, and I struggle why laravel test always inaccurate. To simulate 404 status code, I created a test with non existing route, but it always returns 200 status code.
public function testTicketsLists()
{
$user = factory(User::class)->create();
$response = $this->actingAs($user, 'web')->withSession(['foo' => 'bar'])->get("api/{$this->endpoint}/ticketss");
$response
->assertStatus(404);
// $response->dump();
}
Expected status code 404 but received 200. Failed asserting that 404 is identical to 200.
The ticketss
endpoint is does not exists so I expected a 404 status code
api.php
/*
|---------------------------------------------------------------------------
| services
|---------------------------------------------------------------------------
*/
// get all client api methods
Route::get('services/methods', 'Api\ServicesController@methods');
// get service lists
Route::get('services/lists', 'Api\ServicesController@serviceLists');
// get service details by id per client
Route::get('services/{id}/details', 'Api\ServicesController@details');
// all service ids
Route::get('services/serviceids', 'Api\ServicesController@serviceids');
/*
Update, I did some debugging, now I understand why it doesn't return 404 response
Illuminate\Testing\TestResponse^ {#8672
+baseResponse: Illuminate\Http\JsonResponse^ {#7923
#data: "{"message":"Page Not Found","status":404,"error":true}"
#callback: null
#encodingOptions: 0
+headers: Symfony\Component\HttpFoundation\ResponseHeaderBag^ {#7924
#computedCacheControl: array:2 [
"no-cache" => true
"private" => true
]
#cookies: []
#headerNames: array:5 [
"cache-control" => "Cache-Control"
"date" => "Date"
"content-type" => "Content-Type"
"x-ratelimit-limit" => "X-RateLimit-Limit"
"x-ratelimit-remaining" => "X-RateLimit-Remaining"
]
#headers: array:5 [
"cache-control" => array:1 [
0 => "no-cache, private"
]
"date" => array:1 [
0 => "Thu, 27 Aug 2020 06:38:52 GMT"
]
"content-type" => array:1 [
0 => "application/json"
]
"x-ratelimit-limit" => array:1 [
0 => 60
]
"x-ratelimit-remaining" => array:1 [
0 => 59
]
]
#cacheControl: []
}
#content: "{"message":"Page Not Found","status":404,"error":true}"
#version: "1.1"
#statusCode: 200
#statusText: "OK"
#charset: null
+original: array:3 [
"message" => "Page Not Found"
"status" => 404
"error" => true
]
+exception: null
}
#streamedContent: null
}
The statusCode is always 200, how can I properly test in this scenario ?