【问题标题】:Laravel TDD Failed asserting that an array has the key 'id'Laravel TDD 断言数组具有键“id”失败
【发布时间】:2020-06-20 15:07:15
【问题描述】:

我一直在关注这个series 来了解 Laravel 的 TDD。我来了

断言数组具有键“id”失败。

运行测试命令时出现错误消息。

下面是我的代码:

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
        ]);
        
    }

产品表结构:

id | image | name | price

我正在关注的视频系列是在 laravel 5.7 上制作的,我正在运行 Laravel 7。 这是错误的原因吗?

【问题讨论】:

    标签: php laravel tdd phpunit laravel-7


    【解决方案1】:

    发现我需要使用

    use RefreshDatabase;

    特质。

    【讨论】:

      猜你喜欢
      • 2018-06-03
      • 1970-01-01
      • 1970-01-01
      • 2020-05-08
      • 2019-03-14
      • 2020-07-04
      • 1970-01-01
      • 2011-05-27
      相关资源
      最近更新 更多