'Unit testing with get request
I am trying to create a unit test for one of my api.
In the frontend, I send it this way...
params = {
participants: JSON.stringify(participants),
section: JSON.stringify(section),
};
axios.get('/api/list', params)
while in the controller, it receives the params this way...
public function list(Request $request)
{
$participants = json_decode($request->participants);
$section = json_decode($request->section);
}
Now, I tried making a unit test out of this. by doing...
$params = [
'participants' => ['id', 'name', 'rating'],
'section' => ['id', 'code'],
];
$this->get('/api/list'.http_build_query($params))->assertStatus(200)
// $this->json('/api/list', $params)->assertStatus(200) // -> also tried this one
// $this->getJson('/api/list', $params)->assertStatus(200) // -> also tried this one
// $this->call('GET', '/api/list', $params)->assertStatus(200) // -> also tried this one
But none of them works, it always says TypeError: json_decode(): Argument #1 ($json) must be of type string, array given.
So, the way I built the url and the params must be all wrong,
so my question here is that, what's the correct way of building the url so that it provides a correct url string format and the controller will json_decode the params?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
