簡體   English   中英

PHPUnit assertDatabaseHas 與 Laravel 和慣性

[英]PHPUnit assertDatabaseHas with Laravel and Inertia

我有一個使用 Intertia.js 的 Laravel 8 設置。 我正在嘗試使用assertDatabaseHas()方法測試我的發布路由以存儲新資源並希望斷言數據庫存儲了新記錄。

Controller.php

public function store(SlotCreate $request): RedirectResponse
    {
        $slot = CreateSlot::dispatchNow($request);

        return redirect()->back()->with([
            'message' => 'Slot Created Successfully.',
            'slot' => new SlotResource($slot)
        ]);
    }

CreateSlotTest.php

public function testCreateSlot()
    {
        $response = $this->actingAs($this->teamAdminOne)->post('/slots', [
            'start_time' => '09:00',
            'end_time' => '10:00',
            'max_bookings' => 3
        ]);

        $response->assertStatus(302);

        $this->assertDatabaseHas('slots', [
            'id' => ???
            'team_id' => $this->teamAdminOne->currentTeam(),
            'start_time' => '09:00',
            'end_time' => '10:00',
            'max_bookings' => 3
        ]);
    }

當我調試 session 時,我可以看到我添加到with()方法的插槽,但我不知道如何訪問它。

我想知道我是否在 controller 中正確返回了插槽,如果是,我如何訪問響應以斷言數據庫是否有此記錄?

實際上,當您調用redirect()->with()時,它會通過調用session()->flush()方法將變量添加到 session 中。 因此,在這種情況下,您可以通過Illuminate\Support\Facades\Session外觀檢索變量。

$slot = Illuminate\Support\Facades\Session::get('slot')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM