Laravel Moloquent CRUD操作的单元测试在时间戳和日期上失败

时间:2017-07-19 11:25:54

标签: mongodb laravel moloquent

我正在尝试测试我的CRUD方法,但它失败了因为laravel TestCase方法assertDatabaseHas()无法比较使用timestamp o Date作为类型的字段,因为它被转换为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

0 个答案:

没有答案