[英]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.