I've been following this series to catch up with TDD with Laravel. I'm getting
Failed asserting that an array has the key 'id'.
error message when I run the test command.
Below is my code:
api.php
Route::namespace('API')->group(function(){
Route::post('/products', 'ProductController@store');
});
ProductController.php
namespace App\Http\Controllers\API;
use App\Product;
public function store(Request $request)
{
$product = Product::create([
'name' => $request->name,
'slug' => $request->slug,
'price' => $request->price
]);
return response()->json($product, 201);
}
ProductControllerTest.php
public function can_create_a_product()
{
$faker = Factory::create();
$response = $this->json('POST', '/api/products', [
'name' => $name = $faker->company,
'slug' => str_slug($name),
'price' => $price = random_int(10, 100)
]);
$response->assertJsonStructure([
'id', 'name', 'slug', 'price', 'created_at'
])
->assertJson([
'name' => $name,
'slug' => str_slug($name),
'price' => $price
])
->assertStatus(201);
$this->assertDatabaseHas('products', [
'name' => $name,
'slug' => str_slug($name),
'price' => $price
]);
}
Structure of products table:
id | image | name | price
The video series I'm following is made on laravel 5.7 and I'm running Laravel 7. Is this the cause of error ?