POST 请求的 SilverStripe 蓝宝石功能测试始终返回 404 状态

SilverStripe Sapphire Functional testing on POST request is always returning 404 status

我正在从事 SilverStripe 项目。我正尝试按照此文档 https://docs.silverstripe.org/en/4/developer_guides/testing/functional_testing/ 为我的应用程序编写功能测试。我正在测试 POST 请求。但它不起作用。你可以在下面看到我的代码。

我有一个名为 CustomFormPageController 的控制器 class,代码如下。

class CustomFormPageController extends PageController
{
    private static $allowed_actions = [
        'testPostRequest',
    ];

    private static $url_handlers = [
        'testPostRequest' => 'testPostRequest',
    ];

    public function testPostRequest(HTTPRequest $request)
    {
        if (! $request->isPOST()) {
            return "Bad request";
        }

        return "Request successfully processed";
    }
}

我还有一个名为 CustomFormPage 的控制器页面 class。以下是 class.

的实现
class CustomFormPage extends Page
{

}

我要测试的是我正在尝试测试 testPostRequest 方法 returns 正确的值。

以下是我的测试class

class CustomFormPageTest extends FunctionalTest
{
    protected static $fixture_file = 'fixtures.yml';

    public function testTestingPost()
    {
        $formPage = $this->objFromFixture(CustomFormPage::class, 'form_page');

        $response = $this->post($formPage->URLSegment . '/testPostRequest', [
            'name' => 'testing'
        ]);

        var_dump($response);
    }
}

以下是我的 fixtures.yml 文件。

SilverStripe\CMS\Model\SiteTree:
    sitetree_page_one:
        ID: 1
        ClassName: CustomFormPage
        Title: Page Title 1
        URLSegment: custom-form-page
CustomFormPage:
    form_page:
        ID: 1
        Title: Page Title 1
        URLSegment: custom-form-page

当我 运行 测试时,它总是返回 404 未找到状态,即使该页面是在数据库中创建的。我的代码中缺少什么以及如何修复它?

我刚刚找到了解决方案。我们需要在测试中发布页面如下。

$formPage->publish("Stage", "Live");