[英]PHPUnit assertDatabaseHas with Laravel and Inertia
I have a Laravel 8 setup that utilises Intertia.js.我有一个使用 Intertia.js 的 Laravel 8 设置。 I'm trying to test my post routes to store new resources and want to assert that the database has the new record stored, using the
assertDatabaseHas()
method.我正在尝试使用
assertDatabaseHas()
方法测试我的发布路由以存储新资源并希望断言数据库存储了新记录。
Controller.php 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 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
]);
}
When I debug the session I can see the slot that I added to the with()
method but I can't figure out how to access it.当我调试 session 时,我可以看到我添加到
with()
方法的插槽,但我不知道如何访问它。
I'd like to know if I'm returning the slot correctly in the controller and if yes, how do I access the response to assert if the database has this record?我想知道我是否在 controller 中正确返回了插槽,如果是,我如何访问响应以断言数据库是否有此记录?
Actually when you call redirect()->with()
, it adds the variables into the session by calling the session()->flush()
method.实际上,当您调用
redirect()->with()
时,它会通过调用session()->flush()
方法将变量添加到 session 中。 So in this case you can retrieve the variables via Illuminate\Support\Facades\Session
facade.因此,在这种情况下,您可以通过
Illuminate\Support\Facades\Session
外观检索变量。
$slot = Illuminate\Support\Facades\Session::get('slot')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.