如何测试数据库Laravel?

0yycz8jy  于 2023-03-04  发布在  其他
关注(0)|答案(1)|浏览(184)

我创建了$fake,它有假数据要更新。我的问题是assertDatabaseHas()返回false,因为生成了另一个假数据。我该怎么做,那个假数据在更新表单和数据库中是相同的?

public function test_user_update()
    {
        $admin= $this->user->attachRole('admin');

     $fake = [
         'name'        => fake()->name,
         'city'        => fake()->city,
         'date'        => fake()->date
        ];

        $response = $this->actingAs($admin)->putJson(route('user.update', ['update' => $this->user->id]), $fake);
  
        $this->assertDatabaseHas(User::class, $fake);

    }
7d7tgy0s

7d7tgy0s1#

我使用数据库中的信息执行发布测试,如下所示:

public function test_country_update_api_json(): void
{
    /** @var Country $country */
    $country = Country::factory()->create();
    /** @var User $user */
    $user = User::factory()->create();

    $response = $this
        ->actingAs($user)
        ->patchJson(route('country.update', $country->id), [
            "name" => "test",
        ]);

    $response->assertOk();

    $this->assertTrue(collect($response->decodeResponseJson())
        ->pluck('response.name')->contains('test'));
}

相关问题