I have a laravel 8 Unit Test which looks like this:
public function testAddingTwoCars()
{
$response = $this->postJson('api/basket', ['cars' => [['name' => 'car one'], ['name' => 'car two']]]);
$response
->assertStatus(200)
->assertJson(['total' => 40]);
}
In my route I have:
Route::post('/api/basket',[basketController::class, 'store']);
In my controller I have:
public function store(Request $request)
{
$data = $request->all();
return response()->json($data);
}
when I run the test with php artisan test it shows this:
• Tests\Unit\BasketTest > adding two cars
Unable to find JSON:
[{
"total": 40
}]
within response JSON:
[{
"cars": [
{
"name": "car one"
},
{
"name": "car two"
}
]
}].
How do I:grab the total as its not in $request so that I could do something like this in my controller:
$public function store(Request $request)
{
$data = $request->all();
$data['total'] = 40;
return response()->json($data);
}
The above works, but it is hard coded so is not the best way to do it