2

我正在尝试测试我的 CRUD 方法,但它失败了,因为 laravel TestCase 方法 assertDatabaseHas() 无法比较使用时间戳或日期作为类型的字段,因为它已转换为 MongoDB\BSON\UTCDateTime,是否有任何工作,因为现在我不包括 created_at 和 updated_at 字段,但我都有其他使用此数据类型的模型。

测试代码:

public function testUpdate(){
        $data = factory(Product::class)->create();
        $data->fill(factory(Product::class)->make()->toArray());
        $response = $this->put(route('products.update', $data), $data->toArray());
        $response->assertJson($data->toArray());
        $this->assertDatabaseHas($data->getTable(), $data->toArray());
}

结果错误:

1) Tests\Feature\ProductControllerTest::testUpdate
Failed asserting that a row in the table [products] matches the attributes {
    "name": "CISCO Fire",
    "price": 5461.97,
    "description": "Ea eligendi qui qui debitis soluta nihil itaque. Quia laborum mollitia voluptatem. Aut corrupti excepturi vel quis ut. Architecto eaque voluptates quia enim in exercitationem nesciunt.",
    "updated_at": "2017-07-19 07:38:42",
    "created_at": "2017-07-19 07:38:42",
    "_id": "596f0c82d1f2d724aa03c11f"
}.

Found: [
    {
        "_id": {},
        "name": "ACER MURPHY",
        "price": 10722.72,
        "description": "Est ea et omnis nemo. Et iure ea accusamus distinctio omnis exercitationem. Sapiente et dignissimos eveniet et iusto quos explicabo. Tempore inventore corporis minima minus quibusdam.",
        "updated_at": {},
        "created_at": {}
    },
    {
        "_id": {},
        "name": "TOSHIBA Flame",
        "price": 1314.84,
        "description": "Omnis nisi iusto eum et. Nisi perferendis dolorem sed id et. Odit et sequi corporis necessitatibus error et consequatur.",
        "updated_at": {},
        "created_at": {}
    },
    {
        "_id": {},
        "name": "CISCO Fire",
        "price": 5461.97,
        "description": "Ea eligendi qui qui debitis soluta nihil itaque. Quia laborum mollitia voluptatem. Aut corrupti excepturi vel quis ut. Architecto eaque voluptates quia enim in exercitationem nesciunt.",
        "updated_at": {},
        "created_at": {}
    }
].

/home/ivan/repos/primo-server/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php:22
/home/ivan/repos/primo-server/tests/Feature/ProductControllerTest.php:40
4

0 回答 0