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

问题描述 投票:0回答:1

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

我有一个名为CustomFormPageController的控制器类,带有以下代码。

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 CustomFormPage extends Page
{

}

我要测试的是我要测试testPostRequest方法返回正确的值。

下面是我的测试课

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:
    cision_subscription_form_page_one:
        ID: 1
        Title: Page Title 1
        URLSegment: custom-form-page

我运行测试时,即使页面是在数据库中创建的,它也始终返回404 not found状态。我的代码中缺少什么,我该如何解决?

unit-testing silverstripe functional-testing silverstripe-4
1个回答
0
投票
但是在您的灯具文件中,您的CustomFormPage的句柄为cision_subscription_form_page_one

获取CustomFormPage的代码应为:

$formPage = $this->objFromFixture(CustomFormPage::class, 'cision_subscription_form_page_one');
© www.soinside.com 2019 - 2024. All rights reserved.