gpt4 book ai didi

php - 如何修复 Laravel 8 测试中的 "419 - Page expired"错误?

转载 作者:行者123 更新时间:2023-12-05 04:55:15 67 4
gpt4 key购买 nike

我是 Laravel 新手,正在学习一些教程。

我遇到了一个相当简单的测试练习,测试应该在一个表单中进行简单的发布,然后检查新数据是否在数据库中。

不幸的是,我的测试失败了,因为“post”调用得到的是“Page Expired”答案。

测试代码:

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Tests\TestCase;

class SubmitLinksTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function guest_can_submit_a_new_link()
{
$response = $this->post('/submit', [
'title' => 'Example Title',
'url' => 'http://example.com',
'description' => 'Example description.',
]);

$response->dump();

$this->assertDatabaseHas('links', [
'title' => 'Example Title'
]);

$response
->assertStatus(302)
->assertHeader('Location', url('/'));

$this
->get('/')
->assertSee('Example Title');
}
}

调用 php artisan test 的结果:

...

<div class="px-4 text-lg text-gray-500 border-r border-gray-400 tracking-wider">\n
419 </div>\n \n
<div class="ml-4 text-lg text-gray-500 uppercase tracking-wider">\n
Page Expired </div>\n
</div>\n
</div>\n
</div>\n
</body>\n </html>\n """

FAIL Tests\Feature\SubmitLinksTest ⨯ guest can submit a new link

---

• Tests\Feature\SubmitLinksTest > guest can submit a new link Failed asserting that a row in the table [links] matches the attributes {
"title": "Example Title" }.
The table is empty..

at tests/Feature/SubmitLinksTest.php:28
24▕
25▕ $response->dump();
26▕
27▕ $this->assertDatabaseHas('links', [ ➜ 28▕ 'title' => 'Example Title'
29▕ ]);
30▕
31▕ $response
32▕ ->assertStatus(302)


Tests: 1 failed Time: 0.14s

我尝试了几次 hack(在发布之前调用“$this->get('/submit');”,手动将“APP_DEBUG”设置为 true,将“APP_ENV”设置为测试等)但都没有用。

无论如何,AFAIU,根据 Laravel 的 8 文档,调用“php artisan test”应该注意在测试期间禁用 CSFR token 验证:https://laravel.com/docs/8.x/http-tests#customizing-request-headers

我读过几个类似的问题,提到 Laravel 中的错误 419,但没有一个与测试相关。对它们提出的修复对我不起作用。

当我手动尝试该表单时,它运行良好。只有在被 Laravel 的测试基础架构调用时,我才会收到 419 错误。

我怎样才能使这个简单的测试工作?

最佳答案

试试 artisan cache:clearartisan config:clear

我终于做到了:

我将 /bootstrap/cache/config.php 重命名为 /bootstrap/cache/config.hidden.php

然后它对我有用。

还要考虑这个:https://medium.com/@fatboyxpc/laravel-testing-woes-token-mismatch-d9c00fbef263

关于php - 如何修复 Laravel 8 测试中的 "419 - Page expired"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65459434/

67 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com