Laravel:assertDatabaseHas-意外失败

Laravel:assertDatabaseHas-意外失败,laravel,testing,phpunit,Laravel,Testing,Phpunit,我不明白为什么这个数据库测试失败。我知道我没有对创建的_at和更新的_at列进行断言,但这三个列(id、user\u id、thing\u id)应该足够了,而且我确信我以前只对一组列进行了测试,并且已经成功了 我错过了什么 Failed asserting that a row in the table [thing_history] matches the attributes [ { "id": 1,

我不明白为什么这个数据库测试失败。我知道我没有对创建的_at和更新的_at列进行断言,但这三个列(id、user\u id、thing\u id)应该足够了,而且我确信我以前只对一组列进行了测试,并且已经成功了

我错过了什么

    Failed asserting that a row in the table [thing_history] matches the attributes [
            {
                "id": 1,
                "user_id": 1,
                "thing_id": 1
            },
            {
                "id": 2,
                "user_id": 1,
                "thing_id": 2
            },
            {
                "id": 3,
                "user_id": 1,
                "thing_id": 3
            }
        ].

        Found: [
            {
                "id": "1",
                "user_id": "1",
                "thing_id": "1",
                "created_at": "2019-02-01 21:18:17",
                "updated_at": "2019-02-01 21:18:17"
            },
            {
                "id": "2",
                "user_id": "1",
                "thing_id": "2",
                "created_at": "2019-02-01 21:18:17",
                "updated_at": "2019-02-01 21:18:17"
            },
            {
                "id": "3",
                "user_id": "1",
                "thing_id": "3",
                "created_at": "2019-02-01 21:18:17",
                "updated_at": "2019-02-01 21:18:17"
            }
        ]



This is the test code

    /** @test */
    public function retrieving_feed_creates_history()
    {
        $user = factory('App\User')->create();

        $this->actingAs($user);

        factory('App\Thing', 3)->create();

        $response = $this->json('GET', '/api/thing/feed/all');

        $this->assertDatabaseHas('feed_histories', [
            [
                'id' => 1,
                'thing_id' => 1,
                'user_id' => $user->id,
            ],
            [
                'id' => 2,
                'thing_id' => 2,
                'user_id' => $user->id,
            ],
            [
                'id' => 3,
                'thing_id' => 3,
                'user_id' => $user->id,
            ]
        ]);
    }
这是迁移代码:

  public function up()
        {
            Schema::create('feed_histories', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('user_id');
                $table->integer('thing_id');
                $table->timestamps();
            });
        }

好像我误解了什么。为了检查几行,我必须将测试拆分为每行的独立断言

这很好:

$this->assertDatabaseHas('feed_histories', [
        'thing_id' => $thingA->id,
        'user_id' => $user->id, 
]);

$this->assertDatabaseHas('feed_histories', [
        'thing_id' => $thingB->id,
        'user_id' => $user->id, 
]);

是的,多条记录的映射失败,因为assertDatabaseHas函数当前仅通过映射where子句中的单行来处理单行

为了获得更好的了解,您可以查看assertDatabaseHas的基本函数

public function matches($table): bool
{
    return $this->database->table($table)->where($this->data)->count() > 0;
}
这里,$this->data引用assertDatabaseHas function的第二个参数
因此,它消除了我们为什么不能通过数组数组的疑问。

您可以添加您的测试和迁移表吗,谢谢您添加了测试代码和迁移代码。