我一直在关注这个系列以赶上 Laravel 的 TDD。我越来越
无法断言数组具有键“id”。
运行测试命令时出现错误消息。
下面是我的代码:
api.php
Route::namespace('API')->group(function(){
Route::post('/products', 'ProductController@store');
});
产品控制器.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
]);
}
产品表结构:
id | image | name | price
我正在关注的视频系列是在 laravel 5.7 上制作的,我正在运行 Laravel 7。这是错误的原因吗?