Laravel: assertDatabaseHas - неожиданный сбой - PullRequest
0 голосов
/ 02 февраля 2019

Я не понимаю, почему этот тест базы данных не проходит.Я знаю, что я не утверждаю на столбцы create_at и updated_at, но трех столбцов (id, user_id, thing_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();
            });
        }

1 Ответ

0 голосов
/ 05 февраля 2019

Похоже, я что-то не так понял.Чтобы проверить несколько строк, мне нужно разделить тест на отдельные утверждения для каждой строки.

Это прекрасно работает:

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

$this->assertDatabaseHas('feed_histories', [
        'thing_id' => $thingB->id,
        'user_id' => $user->id, 
]);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...